python - int() 参数必须是字符串、类似字节的对象或数字,而不是 'QuerySet'

标签 python django django-forms

我的工作地址为 this tutorial作为指导。他们的 SignupForm 与我的非常相似,但是当我尝试添加多对多。 (这只是最终将成为多种用户类型的第一步,因此在 MyUser 类上使用 is_xyz 类型 bool 值并不是一个长期解决方案):

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 ?

最佳答案

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

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

admin_user.destination.add(<b>*</b>self.cleaned_data["destination"])
#                          ^

因此,我们在参数前面放置了一个星号 (*)。这样,QuerySet 中的元素就会被解包,并作为单独的元素传递给 .add(..) 函数。

Note: after reading the article, it turns out that the authors used sequence unpacking as well.

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

关于python - int() 参数必须是字符串、类似字节的对象或数字,而不是 'QuerySet',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52451519/

相关文章:

python - 在 Django 中对日期进行分组

python - Django Manytomany 字段对于以 10 为基数的 int() 无效文字 : ''

python/pyside 在 qtreewidget 中使用自定义小部件

python pandas 两个数据帧的复杂合并

python - 减去时间并得到以年为单位的输出,四舍五入到小数点后两位

Django - 类型字符变化的值太长(但似乎在 max_length 限制内)

python - Django Form Error : Select a valid choice. ...不是可用选项之一

django - 防止在表单集验证之前保存模型

python - 如何在 factory-boy 中使用 create_batch 时为属性设置序列?

python - heroku 连接 mysql 数据库