int()参数必须是字符串,类似字节的对象或数字,而不是“ QuerySet”

时间:2018-09-21 22:01:58

标签: python django django-forms

我正在this tutorial的指导下工作。他们的SignupForm与我的非常相似,但是当我尝试添加ManyToMany时收到int() argument must be a string, a bytes-like object or a number, not 'QuerySet'错误。 (这只是最终将成为几种用户类型的第一步,因此在MyUser类上使用is_xyz类型布尔值不是一个长期解决方案):

models.py

class MyUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )

    is_admin = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        # Does the user have a specific permission?
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        # "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        return self.is_admin

    class Meta:
        db_table = 'users_myuser'
        verbose_name = 'MyUser'


class ApplicationUser(models.Model):
    user = models.OneToOneField(MyUser, on_delete=models.CASCADE)


class AdminUser(models.Model):
    user = models.OneToOneField(MyUser, on_delete=models.CASCADE)
    destination = models.ManyToManyField(Destination, blank=True)

forms.py

class GatekeeperPlusSuperCreationForm(forms.ModelForm):

    password1 = CharField(label="Password", widget=PasswordInput)
    password2 = CharField(label="Password confirmation", widget=PasswordInput)

    class Meta:
        model = get_user_model()
        fields = ('email',)

    destination = forms.ModelMultipleChoiceField(queryset=Destination.objects.all(), widget=forms.CheckboxSelectMultiple,
                                             required=True)

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            msg = "Passwords don't match"
            raise forms.ValidationError("Password mismatch")
        return password2

    def save(self, commit=True):
        user = super(GatekeeperPlusSuperCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        user.is_admin = True
        user.save()

        admin_user = AdminUser.objects.create(user=user)
        admin_user.destination.add(self.cleaned_data["destination"])

        return admin_user

我在admin_user.destination.add(self.cleaned_data["destination"]) 行收到错误。当我尝试过print(self.cleaned_data["destination"])时,的确表明它是一个QuerySet,但是为什么呢?

如何从destination = forms.ModelMultipleChoiceField(queryset=Destination.objects.all(), widget=forms.CheckboxSelectMultiple, required=True)中获取值以将其另存为ManyToManyField?

1 个答案:

答案 0 :(得分:0)

因为destination允许您选择多个值,所以它不是单个对象,而是多个。

不过,您可以通过序列拆包轻松解决此问题:

admin_user.destination.add(*self.cleaned_data["destination"])
#                          ^

因此,我们在参数前面加了一个星号(*)。这样QuerySet中的元素将被解压缩,并作为单独的元素传递到.add(..)函数中。

  

注意:在阅读了这篇文章之后,事实证明作者也使用了序列解压缩。

请注意,Django支持使用save_m2m [Django-doc]函数保存多对多关系。