ValueError:“在使用多对多关系之前,需要为字段”id“设置一个值”

时间:2017-06-28 13:37:25

标签: python django

我创建了一个扩展用户模型,我在注册页面上使用了该模型。这两个自定义模型是“Cristin”和一个名为“Rolle”的多对多字段(该人员在组织中的角色)。

我添加了多对多字段后,收到以下错误消息: / accounts / signup /上的ValueError - “Userextended: username_that_I_registered_with ”需要有一个值可以使用此多对多关系之前的字段“id”。

我现在用Google搜索了三个小时,似乎解决方法是在保存模型之前在用户ID上使用 commit = False 。但是,我是Django和Python的新手,并且难以确定我应该在哪里编写代码,而且到目前为止我的尝试都没有成功。如果您可以帮我解决这个问题,请通过代码提供解决方案。

我的 models.py 文件:

class User(auth.models.User,auth.models.PermissionsMixin):
def __str__(self):
    return "@{}".format(self.username)

class Personrolle(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    persrolle = models.CharField(max_length = 30, verbose_name="Role")
    def __str__(self):
        return self.persrolle

class Userextended(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    cristin = models.IntegerField(blank=True, null=True)
    rolle = models.ManyToManyField(Personrolle)

    def __str__(self):
        return self.user.username

...

我的 forms.py 文件:

...

from django import forms
from django.contrib.auth.models import User
from accounts.models import Userextended
from accounts.models import Personrolle
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):

    cristin = forms.IntegerField(required=False)
    rolle = forms.ModelChoiceField(queryset=Personrolle.objects.all())

    class Meta():
        fields = ('first_name','last_name','username','email','password1','password2')
        model = User
        labels = {'username': 'Email',
                  'first_name': 'First name',
                  'last_name': 'Last name',
                  'email': 'Confirm email',
                  'password1': 'Password',
                  'password2': 'Confirm password',
                  'cristin': 'Cristin-ID',
                  'rolle': 'Rolle'}

    def save(self, commit=True):
        if not commit:
            raise NotImplementedError("Can't create User and Userextended without database save")
        user = super(UserCreateForm, self).save(commit=True)
        user_profile = Userextended(user=user,cristin=self.cleaned_data['cristin'],rolle=self.cleaned_data['rolle'])
        user_profile.save()
        return user

...

错误的更详细说明

  • 请求方法:POST
  • 请求网址:http://127.0.0.1:8000/accounts/signup/
  • Django版本:1.11.2
  • 异常类型:ValueError
  • 异常值: 在使用多对多关系之前,“Userextended:username_that_I_registered_with”需要具有字段“id”的值。

  • 异常位置: 中的... / anaconda / envs / myEnv / lib / python3.6 / site-packages / django / db / models / fields / related_descriptors.py, 830行

  • Python可执行文件:... / anaconda / envs / myEnv / bin / python

  • Python版本:3.6.1

我的追溯:

File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

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

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

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

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

File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/views/generic/edit.py" in post
  217.         return super(BaseCreateView, self).post(request, *args, **kwargs)

File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/views/generic/edit.py" in post
  183.             return self.form_valid(form)

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

File "…PROJECT/accounts/forms.py" in save
  57.         user_profile = Userextended(user=user,cristin=self.cleaned_data['cristin'],rolle=self.cleaned_data['rolle'])

File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/base.py" in __init__
  566.                             _setattr(self, prop, kwargs[prop])

File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py" in __set__
  536.         manager = self.__get__(instance)

File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py" in __get__
  513.         return self.related_manager_cls(instance)

File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py" in __init__
  830.                                  (instance, self.pk_field_names[self.source_field_name]))

Exception Type: ValueError at /accounts/signup/
Exception Value: "<Userextended: testytester@yahoo.com>" needs to have a value for field "id" before this many-to-many relationship can be used.

3 个答案:

答案 0 :(得分:1)

您只需在表单中编辑保存方法,

def save(self, commit=True): 
    if not commit: 
        raise NotImplementedError("Can't create User and Userextended without database save") 
    user = super(UserCreateForm, self).save(commit=True) 
    user_profile = Userextended(user=user,cristin=self.cleaned_data['cristin']) 
    user_profile.save() 
    user_profile.rolle.add(self.cleaned_data['rolle'])
    user_profile.save()
    return user

您需要先保存UserExtended模型,然后将Rolle个实例添加到多对多关系中。

答案 1 :(得分:0)

如果您使用的是DRF,则可以使用validate方法:

代替此

    def create(self, validated_data):
        validated_data['slug'] = slugify(validated_data['name'])
        return budgets.models.BudgetCategory.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.slug = slugify(validated_data['name'])
        instance.save()
        return instance

执行此操作

def validate(self, data):
        data['slug'] = slugify(data['name'])
        return data

答案 2 :(得分:-1)

错误消息的发生是因为&#34; rolle&#34;应该被指定为多对一关系而不是多对多关系。该模型已更新至以下版本,现在运行良好:

class Userextended(models.Model):
    ...
    rolle = models.ForeignKey(Personrolle, models.SET_NULL, blank=True, null=True)