<?xml version="1.0"?>
<rss version="2.0"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:dcterms="http://purl.org/dc/terms/" >
<channel>
<title>tag django</title>
<link>http://blog.spang.cc/tags/django/</link>
<description>blog</description>
<item>

	<title>Christine Spang: Django form fields with the same name as Python keywords</title>
	<dcterms:creator>Christine Spang</dcterms:creator>


	<guid isPermaLink="false">http://blog.spang.cc/posts/Django_form_fields_with_the_same_name_as_Python_keywords/</guid>

	<link>http://blog.spang.cc/posts/Django_form_fields_with_the_same_name_as_Python_keywords/</link>


	<category>tags/django</category>

	<category>tags/planet-debian</category>

	<category>tags/python</category>


	<pubDate>Fri, 04 Jun 2010 00:58:11 +0000</pubDate>
	<dcterms:modified>2010-06-04T03:50:45Z</dcterms:modified>

	<description>&lt;p&gt;For &lt;a href=&quot;http://www.ksplice.com/&quot;&gt;work&lt;/a&gt; recently I&#39;ve been doing some
Django-related tasks that involve talking to an external API with
POSTed forms. Django forms objects are declared by creating a class that
inherits from &lt;code&gt;django.forms.Form&lt;/code&gt;, with the fields of the
form declared by declaring attributes of that class. Which works well
and is clean and easy to remember&amp;mdash;unless the API you&#39;re working
with requires a field with the same name as a Python keyword, such as
&lt;code&gt;return&lt;/code&gt;. You can&#39;t declare a field like this as an
attribute; it will trigger a syntax error.&lt;/p&gt;

&lt;p&gt;I spent some time scratching my head over this, and came up with this as
a workaround after source-diving to find out how &lt;code&gt;Form&lt;/code&gt;
objects actually work:&lt;/p&gt;

&lt;pre class=&quot;hl&quot;&gt;&lt;span class=&quot;hl kwa&quot;&gt;from&lt;/span&gt; django &lt;span class=&quot;hl kwa&quot;&gt;import&lt;/span&gt; forms

&lt;span class=&quot;hl kwa&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hl kwd&quot;&gt;ExampleForm&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;(&lt;/span&gt;forms&lt;span class=&quot;hl opt&quot;&gt;.&lt;/span&gt;Form&lt;span class=&quot;hl opt&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;hl kwa&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;hl kwd&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; data&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hl kwa&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; files&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hl kwa&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; auto_id&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hl str&quot;&gt;&#39;id_%s&#39;&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; prefix&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hl kwa&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt;
            initial&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hl kwa&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; errorclass&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;ErrorList&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; label_suffix&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hl str&quot;&gt;&#39;:&#39;&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; empty_permitted&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hl kwa&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; return_url&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hl kwa&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;):&lt;/span&gt;
        forms&lt;span class=&quot;hl opt&quot;&gt;.&lt;/span&gt;Form&lt;span class=&quot;hl opt&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl kwd&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; data&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; files&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; auto_id&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; prefix&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; initial&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt;
            errorclass&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; label_suffix&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; empty_permitted&lt;span class=&quot;hl opt&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;hl kwa&quot;&gt;if&lt;/span&gt; return_url &lt;span class=&quot;hl kwa&quot;&gt;is not None&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;:&lt;/span&gt;
            self&lt;span class=&quot;hl opt&quot;&gt;.&lt;/span&gt;fields&lt;span class=&quot;hl opt&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hl str&quot;&gt;&#39;return&#39;&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;] =&lt;/span&gt; forms&lt;span class=&quot;hl opt&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;hl kwd&quot;&gt;CharField&lt;/span&gt;&lt;span class=&quot;hl opt&quot;&gt;(&lt;/span&gt;widget&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;forms&lt;span class=&quot;hl opt&quot;&gt;.&lt;/span&gt;HiddenInput&lt;span class=&quot;hl opt&quot;&gt;,&lt;/span&gt; initial&lt;span class=&quot;hl opt&quot;&gt;=&lt;/span&gt;return_url&lt;span class=&quot;hl opt&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;It turns out that the attribute declaration is just syntactic sugar for
creating a dictionary of key/value pairs, which is then stored in the
&lt;code&gt;fields&lt;/code&gt; attribute. So we can monkeypatch in extra values after
the translation. Which is somewhat more awkward and ugly, but works in a pinch.&lt;/p&gt;

&lt;p&gt;Note that I haven&#39;t extensively tested what interactions this may cause with
other forms code, so use with some caution.&lt;/p&gt;
</description>


	<comments>/posts/Django_form_fields_with_the_same_name_as_Python_keywords/#comments</comments>

</item>

</channel>
</rss>
