简体   繁体   中英

Django won't save ManyToMany field on ModelForm

I've got a model similar to the following (this is a condensed version of a long form):

class UserProfile(models.Model):
    display_name = models.CharField()
    nationality = models.ForeignKey(Nationality)
    religion = models.ForeignKey(Religion)

    partner_nationality = models.ManyToManyField(
        Nationality,
        related_name='partner_nationality',
        blank=True)
    partner_religion = models.ManyToManyField(
        Religion,
        related_name='partner_religion',
        blank=True)

And the following model form:

class UserProfilePartnerPreferencesForm(ModelForm):
    partner_nationality = ModelChoiceField(
        queryset=Nationality.objects.order_by('name'),
        widget=CheckboxSelectMultiple,
        empty_label=None,
        required=False,
        to_field_name='id')

    class Meta:
        model = UserProfile
        fields = [
            'partner_religion',
            'partner_nationality',
        ]
        widgets = {
            'partner_religion': CheckboxSelectMultiple(),
        }

And this generic view:

class UserProfilePartnerPreferencesUpdateView(LoginRequiredMixin,
                                              UpdateView):
    model = UserProfile
    form_class = UserProfilePartnerPreferencesForm

    def get(self, request, *args, **kwargs):
        """
        Force the PK of the object to edit to the current user's profile
        """
        self.kwargs[self.pk_url_kwarg] = request.user.profile.id
        return super(UserProfilePartnerPreferencesUpdateView, self).get(
            request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """
        Force the PK of the object to edit to the current user's profile
        """
        self.kwargs[self.pk_url_kwarg] = request.user.profile.id
        return super(UserProfilePartnerPreferencesUpdateView, self).post(
            request, *args, **kwargs)

So what I'm doing in the model form is:

  • Not displaying some fields (the display_name )
  • Changing the widget on a field
  • Creating a custom queryset and changing the widget on a field.

This form displays OK but the field where I've passed a custom queryset won't save. If I don't give any values for it, django complains with 'NoneType' object is not iterable while trying to save the field ( partner_nationality in this example). If I give it a valid value, it says that the value isn't valid.

So it seems like fields where I supply a custom queryset aren't being applied correctly when the form is saved. If I comment out the customisation (ie partner_nationality in the ModelForm), it saves correctly with the default settings.

How can I pass a customised queryset and change the widget for a model's ManyToMany field? Also, bonus points if there's a simpler way (something like passing a parameter to the widgets dict (where partner_religion is defined).

I'm using Django 1.11.1.

Update

Full traceback is as follows when I don't select any options:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/user-profile/edit/preferences

Django Version: 1.11.1
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'dating.dating',
 'dating_site']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/venv/lib/python3.5/site-packages/django/contrib/auth/mixins.py" in dispatch
  56.         return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)

File "/venv/lib/python3.5/site-packages/django/contrib/auth/mixins.py" in dispatch
  116.         return super(UserPassesTestMixin, self).dispatch(request, *args, **kwargs)

File "/venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/dating/dating/dating/views/user_profile.py" in post
  99.         return super(UserProfilePartnerPreferencesUpdateView, self).post(request, *args, **kwargs)

File "/venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
  240.         return super(BaseUpdateView, self).post(request, *args, **kwargs)

File "/venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post
  183.             return self.form_valid(form)

File "/venv/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid
  162.         self.object = form.save()

File "/venv/lib/python3.5/site-packages/django/forms/models.py" in save
  452.             self._save_m2m()

File "/venv/lib/python3.5/site-packages/django/forms/models.py" in _save_m2m
  434.                 f.save_form_data(self.instance, cleaned_data[f.name])

File "/venv/lib/python3.5/site-packages/django/db/models/fields/related.py" in save_form_data
  1686.         getattr(instance, self.attname).set(data)

File "/venv/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py" in set
  982.             objs = tuple(objs)

Exception Type: TypeError at /events/dating/user-profile/edit/preferences
Exception Value: 'NoneType' object is not iterable

In your model, partner_nationality is a ManyToManyField . Therefore you should use a ModelMultipleChoiceField in your form, not a ModelChoiceField .

class UserProfilePartnerPreferencesForm(ModelForm):
    partner_nationality = ModelMultipleChoiceField(
        queryset=Nationality.objects.order_by('name'),
        widget=CheckboxSelectMultiple,
    )

If you define the widget in the field definition, you don't have to set it in widgets as well.

Another approach is to change the field's attributes in the __init__ method. For example, you can change the queryset with the following:

class UserProfilePartnerPreferencesForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserProfilePartnerPreferencesForm, self).__init__(*args, **kwargs)
        self.fields['partner_nationality'].queryset = Nationality.objects.order_by('name')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM