我正在从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)
表格
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(*self.cleaned_data["destination"])
# ^
因此,我们在参数前面加了一个星号(
*
)。这样,QuerySet
中的元素将被解压缩,并作为单独的元素传递给.add(..)
函数。注意:阅读本文后,事实证明作者还使用了序列解压缩。
请注意,Django支持使用
save_m2m
[Django-doc]函数保存多对多关系。关于python - int()参数必须是字符串,类似字节的对象或数字,而不是“QuerySet”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52451519/