Problems with ManyToManyFields from Base and Derived Classes

71 views
Skip to first unread message

Alessandro Caruso

unread,
Apr 2, 2014, 9:12:42 AM4/2/14
to django...@googlegroups.com
Hi all.


the code:

the problem:
When editing a Videoconference record through the VideoconferenceForm, Django returns an error: "Cannot resolve keyword 'videoconference' into field. Choices are:" followed by the list of the fields of the User object.


Oddly the problem shows only when:
- running the code under Apache with DEBUG=False (it all works great when DEBUG=True or when running under Django dev server regardless of DEBUG Value)
- I try to edit the form (i.e. it works as aspected when inserting a new record with no Videoconference instance passed to the form)

If I remove the viewers filed from the Videoconference class the problem disappears (in that case the class is no different from Room, PersonalComputer or Projector classes)
I've tried to use related_name with both managers field of the Asset class and viewers of the Videoconference class but with no success.

I hope I've been clear enough.

Thanks in advance,
Alex


Liam Thompson

unread,
Apr 2, 2014, 10:22:05 AM4/2/14
to django...@googlegroups.com
Hi Alex

Your code is way more advanced that my skill level. Was wondering if you had tried to run that query

viewers = ModelMultipleChoiceField(queryset = User.objects.all().order_by('username'))

from the shell ?

Liam

Alessandro Caruso

unread,
Apr 2, 2014, 12:11:47 PM4/2/14
to django...@googlegroups.com
Hi Liam,
No I didn't try, but when the form is loaded empty (to create a new record) I can see the Multiple Select List widget with all the users (order by username) and I can even save one or more of them as viewers of the asset.

Do you think I should try it anyway? I mean should I expect some sort of error or do other test on the returned value ?

Liam J Thompson

unread,
Apr 2, 2014, 12:30:11 PM4/2/14
to django...@googlegroups.com
Hi Alex

You might try that command, it's quick through the shell.

I suppose there are many ways to do forms, but I've populated my
modelforms through an __init__ to make sure its loaded fresh for every
form requested, not just on the loading of the server. So object versus
class if that makes sense.

Does it make a difference in your case ? I don't know, I'm still new at
this so making some suggestions.

Liam




Alessandro Caruso

unread,
Apr 3, 2014, 12:20:58 PM4/3/14
to django...@googlegroups.com
I got the traceback (thanks to a middleware which collects it and a send it back to the ajax request even if DEBUG=False)

Environment:


Request Method: POST

Django Version: 1.4.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'south',
 'beeusers',
 'beeactions',
 'assets',
 'reservations',
 'ldapusers',
 'tickets',
 'videoconferences')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'assets.middleware.ExceptionMiddleware')


Traceback:
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/srvadmin/prenota_dev/assets/views.py" in asset_edit
  107. form = VideoconferenceForm(instance=obj)
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/forms/models.py" in __init__
  238.             object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/forms/models.py" in model_to_dict
  128.                 data[f.name] = [obj.pk for obj in f.value_from_object(instance)]
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/db/models/fields/related.py" in value_from_object
  1272.         return getattr(obj, self.attname).all()
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/db/models/manager.py" in all
  116.         return self.get_query_set()
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/db/models/fields/related.py" in get_query_set
  567.                 return super(ManyRelatedManager, self).get_query_set().using(db)._next_is_sticky().filter(**self.core_filters)
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/db/models/query.py" in filter
  624.         return self._filter_or_exclude(False, *args, **kwargs)
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  642.             clone.query.add_q(Q(*args, **kwargs))
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
  1250.                             can_reuse=used_aliases, force_having=force_having)
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_filter
  1122.                     process_extras=process_extras)
File "/home/srvadmin/.virtualenvs/prenota_dev/lib/python2.7/site-packages/django/db/models/sql/query.py" in setup_joins
  1316.                             "Choices are: %s" % (name, ", ".join(names)))

Exception Type: FieldError at /assets/edit/videoconference/30
Exception Value: Cannot resolve keyword 'viewed_videoconferences' into field. Choices are: action, beeuser, date_joined, email, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, managed_assets, password, reservation, ticket, user_permissions, username


The only differences from the original code are the two related_names (managed_assets for managers and viewed_videoconferences for viewers).

It seems to me that Django is trying to save a User object throwing an error because it can't find a viewed_videoconferences field in the User model (obviously)

Liam J Thompson

unread,
Apr 3, 2014, 1:05:28 PM4/3/14
to django...@googlegroups.com
Hi Alex

Let me have another look through your code and I'll try to come up with
something useful.

Liam
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/DL1EHxT_5EQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/add83ad6-c092-47f2-9ef4-2957760b4cda%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/add83ad6-c092-47f2-9ef4-2957760b4cda%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Alessandro Caruso

unread,
Apr 4, 2014, 2:44:17 AM4/4/14
to django...@googlegroups.com
Thanks Liam

Liam J Thompson

unread,
Apr 4, 2014, 6:26:25 AM4/4/14
to django...@googlegroups.com
Hi Alex

My first thought is that this has something to do with the M2M field
stated in Class Videoconference. I'm not 100% sure though, I've only
recently started using this type of relationship and I find them rather
confusing.

I didn't understand why ''' models.ManyToManyField('auth.User'... ''' in
VideoConference(Asset) is written with auth.User as a text field, is
this because the relationship has not been created yet ? Why not just User ?


What if you put the "managers" and "viewers" in an __init__ and send
those initial values from your view, or just move your db queries into
an __init__ ?

class VideoconferenceForm(ModelForm):
def __init__(self, *args, **kwargs):

self.managers = kwargs.pop('', None)
self.viewers = kwargs.pop('', None)

super(FileForm, self).__init__(*args, **kwargs)
self.fields['managers'] =
forms.ModelMultipleChoiceField(choices=self.managers, required=False)
self.fields['viewers'] =
forms.ModelMultipleChoiceField(choices=self.viewers, required=False)

Sorry I can't be more helpful, your code looks like you know more than
me anyway.

Liam
> > 128. data[f.name <http://f.name>] = [obj.pk
> <http://obj.pk> for obj in
> <https://groups.google.com/d/topic/django-users/DL1EHxT_5EQ/unsubscribe>.
>
> > To unsubscribe from this group and all its topics, send an email to
> > django-users...@googlegroups.com <javascript:>
> > <mailto:django-users...@googlegroups.com <javascript:>>.
> > To post to this group, send email to django...@googlegroups.com
> <javascript:>
> > <mailto:django...@googlegroups.com <javascript:>>.
> <http://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/add83ad6-c092-47f2-9ef4-2957760b4cda%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/DL1EHxT_5EQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/42911649-b742-4042-9537-1594792c3ce3%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/42911649-b742-4042-9537-1594792c3ce3%40googlegroups.com?utm_medium=email&utm_source=footer>.

Alessandro Caruso

unread,
Apr 4, 2014, 6:36:17 AM4/4/14
to django...@googlegroups.com
Hi Liam,
first let me thank you for your time.

The relationship in Videoconference is written using the explicit string form because at first I thought it could be a circular import problem, but it could be written as User without affecting the problem positively.

I'll try to init the form class as suggested and see if it makes the error disappear and I'll let you know.
>      > <mailto:django-users+unsub...@googlegroups.com <javascript:>>.
>      > To post to this group, send email to django...@googlegroups.com
>     <javascript:>
>      > <mailto:django...@googlegroups.com <javascript:>>.
>      > Visit this group at http://groups.google.com/group/django-users
>     <http://groups.google.com/group/django-users>.
>      > To view this discussion on the web visit
>      >
>     https://groups.google.com/d/msgid/django-users/add83ad6-c092-47f2-9ef4-2957760b4cda%40googlegroups.com
>     <https://groups.google.com/d/msgid/django-users/add83ad6-c092-47f2-9ef4-2957760b4cda%40googlegroups.com>
>
>      >
>     <https://groups.google.com/d/msgid/django-users/add83ad6-c092-47f2-9ef4-2957760b4cda%40googlegroups.com?utm_medium=email&utm_source=footer
>     <https://groups.google.com/d/msgid/django-users/add83ad6-c092-47f2-9ef4-2957760b4cda%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>      > For more options, visit https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/DL1EHxT_5EQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users...@googlegroups.com

Alessandro Caruso

unread,
Apr 4, 2014, 12:56:11 PM4/4/14
to django...@googlegroups.com
I've moved the forms definition into a separated forms.py file (as suggested by a member of the italian group) and it seems to work.
I'll do some tests in the next days then post the correct version.
Reply all
Reply to author
Forward
0 new messages