首页 > 解决方案 > 将复选框值存储在 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

我已经工作了第二天。我在这里想念什么?

标签: djangoreactjsserialization

解决方案


推荐阅读