django - 将复选框值存储在 db 的新列中 - react/django
问题描述
我一直在尝试将从表单发送的一个复选框promo_consent的值存储到我刚刚在数据库中创建的新列中。
无论我做什么,无论复选框是否被选中,它总是存储 TRUE,或者无论复选框是否被选中,它总是存储 FALSE。
我有这个模型:
class User(AbstractBaseUser, PermissionsMixin, PagePermissionMixin):
"""User model for both staff and clients.
It consists of base AbstractBaseUser class and has 2 permissions mixins.
One of them is for standard django permissions and the second is
for Page object permissions.
Note:
This model is used for OAuth2 and Django authentication.
"""
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
email = models.EmailField(_('email address'), unique=True, blank=True)
new_email = models.EmailField(_('new email'), blank=True)
is_staff = models.BooleanField(
_('staff status'),
default=False,
help_text=_('Designates whether the user can log into this admin '
'site.'))
is_active = models.BooleanField(
_('active'),
default=True,
help_text=_('Designates whether this user should be treated as '
'active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(
_('date joined'),
default=timezone.now)
promo_consent = models.BooleanField(
_('Promo consent'),
default=False,
help_text=_('Shows whether user has agreed to be notified about '
'news and promo sales'))
....
....
....
这个序列化器:
class RegistrationSerializer(SerializerSchemaMixin, serializers.Serializer,
SerializerValidateMixin,
EmailUniquenessValidationMixin,
PasswordValidationMixin):
"""Registration serializer."""
first_name = serializers.CharField(max_length=30, default='')
last_name = serializers.CharField(max_length=30, default='')
email = serializers.EmailField(required=True)
password = serializers.CharField(max_length=100, required=True)
password2 = serializers.CharField(max_length=100, required=True)
rules = serializers.BooleanField(required=True)
promo_consent = serializers.BooleanField(required=False)
def validate_rules(self, value):
"""Checks if 'rules' is True."""
if not value:
raise serializers.ValidationError(_('Rules has to be checked'))
else:
return value
def promo_consent(self, value):
return true
def validate(self, data):
"""Override serializer.validate()."""
self.validate_passwords_uniformity(data)
return data
def save(self, **kwargs):
"""Register new user and send confirmation email."""
language = kwargs['language']
email = self.validated_data['email']
promo_consent = self.promo_consent
self.instance = User(first_name=self.validated_data['first_name'],
last_name=self.validated_data['last_name'],
email=email,
is_active=False,
email_confirmation=uuid.uuid4().hex)
self.instance.set_password(self.validated_data['password'])
self.instance.save()
self.instance.send_email_confirmation(language, email)
return self.instance
我已经工作了第二天。我在这里想念什么?
解决方案
推荐阅读
- flutter - SetState 不更新参数 Flutter 的值 ?? 我想在网格之间切换,但 setstate 只显示网格视图。?? 我如何更新状态
- function - OCaml:处理列表元素和 int 之间奇偶校验的递归函数
- angular - 具有动态标头授权的 Angular Apollo WebSocketLink
- javascript - Electron exec 启动多个进程
- r - 从 R 中查找 pandoc 可执行文件
- html - 通过 a 标签获取一条线
- java - querydsl:java.lang.NoSuchFieldError:TREATED_PATH
- c# - 如何使用 tao.openGL 移动和旋转相机
- php - Afterpay支付网关响应问题
- python - Python text-to-speech and speech-recognition at the same time