Is this a bug in the documentation? ModelForm 'fields' attributes throwing error without trailing comma in the Tuple

8 views
Skip to first unread message

Kurtis

unread,
May 17, 2012, 1:22:04 AM5/17/12
to django...@googlegroups.com
Hey guys,

I've ran into this problem before and it took me a while to figure out the issue. I've ran into it again while just playing around with a small app.

In the ModelForm you can define which fields to use. The documented example is here: https://docs.djangoproject.com/en/1.4/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form

In the 'exclude' attribute, they show the leading comma. In the 'fields' attribute, they don't. I haven't ran their exact example -- but something very similar and I get a weird error, unless I throw in the trailing comma after the final element of the tuple.

I'm just wondering if anyone can verify that this is a bug with the documentation before I start the journey of searching through the existing tickets and filing one if it hasn't been reported.

Thanks!

Irfan Baig

unread,
May 17, 2012, 2:01:55 AM5/17/12
to django...@googlegroups.com
They put a trailing comma in that example because they're writing a tuple with only one element, and the trailing comma is syntactically required in Python to disambiguate from other types of expression. See:
http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences

"A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective."

Kurtis Mullins

unread,
May 17, 2012, 3:14:05 AM5/17/12
to django...@googlegroups.com
Irfan,

I had a feeling it was something along those lines -- although I couldn't find a source for that information. It's definitely not a bug in the Django Documentation -- although it may be a helpful mention :) Then again, I guess a better Python foundation before jumping into Django wouldn't hurt, either.

Thanks a lot!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/0zozPrB0WrQJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages