Django Admin doesn't show all fields from model when registered.

3,022 views
Skip to first unread message

Tundebabzy

unread,
Aug 16, 2012, 11:01:52 AM8/16/12
to django...@googlegroups.com
Hi guys, here's bit of my code http://dpaste.com/hold/787292/

The problem is that my Answer model in admin is missing the 'is_correct', 'date_added' and 'modified' fields.


I tried adding:     
fieldsets = [
        (None,               {'fields': ['text','is_correct','explanation']}),
    ] to my AnswerInLine definition but that throws an ImproperlyConfigured error saying:
'AnswerInline.fieldsets[0][1]['fields']' refers to field 'is_correct' that is missing from the form.

Can anyone help out?


Babatunde Akinyanmi

unread,
Aug 16, 2012, 3:00:56 PM8/16/12
to django...@googlegroups.com
Ok I fixed it and in case someone else falls into such a trap, here's
how I fixed it.

My Answer model had a field with name 'is_correct' and had a method
with name 'is_correct'. This confused django such that:

>>> ans = Answer.objects.get(id=1)
>>> ans.is_correct
<bound method Answer.is_correct of <Answer: Boo>>

So I changed the name of the 'is_correct' method and running the above
in the code threw an error: .....DatabaseError: column
examprep_answer.is_correct does not exist.....

Drop my database and syncdb-ing got things back to normal.

I think django should be able to detect such things and raise an error
or there should be a note in the documentation warning people not to
name their model methods with model field names.

On 8/16/12, Tundebabzy <tunde...@gmail.com> wrote:
> Hi guys, here's bit of my code http://dpaste.com/hold/787292/
>
> The problem is that my Answer model in admin is missing the 'is_correct',
> 'date_added' and 'modified' fields.
>
>
> <https://lh4.googleusercontent.com/-FuxmFXdGVP4/UC0K_dwpIhI/AAAAAAAAADI/deTk9J_tQOE/s1600/Screenshot+from+2012-08-16+15%3A57%3A35.png>
>
> I tried adding:
> fieldsets = [
>
> (None, {'fields':
> ['text','is_correct','explanation']}),
>
> ]
> to my AnswerInLine definition but that throws an ImproperlyConfigured error
> saying:
> 'AnswerInline.fieldsets[0][1]['fields']' refers to field 'is_correct' that
> is missing from the form.
>
> Can anyone help out?
>
>
> --
> 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/-/qK1kFc-11lQJ.
> 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.
>
>

--
Sent from my mobile device

Thomas Orozco

unread,
Aug 17, 2012, 3:38:06 AM8/17/12
to django...@googlegroups.com

This is not really django specific, several objects bearing the same name in the same namespace will always lead to trouble ; )
(And this is not even python specific)

Glad you could fix it and thanks for posting your solution in case someone else has the same issue though. : )

bruno desthuilliers

unread,
Aug 18, 2012, 5:03:47 AM8/18/12
to django...@googlegroups.com


Le jeudi 16 août 2012 21:00:56 UTC+2, Tundebabzy a écrit :
Ok I fixed it and in case someone else falls into such a trap, here's
how I fixed it.

My Answer model had a field with name 'is_correct' and had a method
with name 'is_correct'.


Everything in Python is an object, including functions, classes, modules etc. In a class statement's body, if you first bind name "is_correct" to something (here a field but it could have been anything) then to something else (here a function but it could have been anything too), then the second binding will replace the first one.

 

I think django should be able to detect such things and raise an error

"Django" (that is, in this case, django models metaclass) doesn't even know about this - all it gets is the namespace (ie: a dict) obtained by evaluating the class statement's body. This is how Python work, and expecting something else just means that you assume it works like some other language you already know (bad luck: no two languages work the same).
 
or there should be a note in the documentation warning people not to
name their model methods with model field names.


Why should Django documents Python features ? Python is already (and quite extensively) documented on it's own : http://docs.python.org/



 

Babatunde Akinyanmi

unread,
Aug 18, 2012, 6:15:08 AM8/18/12
to django...@googlegroups.com
Thomas' reply actually did help me narrow down my search efforts to 'namespaces' which is a term I'm actually new to (as a noob) and I was able to get links to articles that did make me understand a little bit more of what goes on beneath the hood of my program especially this: http://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces . My previous google-fu had been failing me and now I do agree that it has nothing to do with django or python and my earlier request can just be ignored.

--
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/-/fspuOAnnWwIJ.

Babatunde Akinyanmi

unread,
Aug 18, 2012, 6:16:27 AM8/18/12
to django...@googlegroups.com
oops......clicked send too soon

....and thanks Thomas and Bruno
Reply all
Reply to author
Forward
0 new messages