NOT NULL约束失败:accounts_sellerprofile.user_id

时间:2019-12-25 22:12:39

标签: django django-models django-rest-framework django-views

我正在尝试创建个人资料表单(两种类型的用户),但是当我使用SellerProfileSerializer创建用户时,它给了我NOT NULL约束失败:accounts_sellerprofile.user_id,我不想放null = True,blank =在user=models.OneToOne字段中为True,因为这是关键信息,这是我的代码。预先感谢

Models.py

class User(AbstractUser):
    username = None
    email = models.EmailField(max_length=100,default=None,unique=True)
    password=models.CharField(max_length=10,default=None)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def __str__(self):
        return self.email   

class SellerProfile(models.Model):

    user = models.OneToOneField(User,null=False,on_delete=models.CASCADE,default=None)
    company_name = models.CharField(max_length= 100,unique=True)
    company_phone = models.CharField(max_length= 100)
    company_zip = models.CharField(max_length= 50)
    company_phone =  models.CharField(max_length= 50)
    seller_type = models.CharField(max_length = 50 ,choices =SELLER_CHOICES,default ='1')
    seller_product = models.CharField(max_length = 50 ,choices =SELLERPRODUCT_CHOICES,default ='1')

    def __str__(self):
        return self.company_name  

Serializers.py

class UserCreateSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id','email', 'password')
    def create(self, validated_data):
        user = User( email=validated_data['email'],)
        user.set_password(validated_data['password'])
        user.save()
        return user
class SellerProfileCreateSerializer(serializers.ModelSerializer):
    user = UserCreateSerializer()

    ****def create(self, validated_data):
        user_data=validated_data.pop('user')
        caretaker = SellerProfile.objects.create(**validated_data)
        User.objects.create(caretaker=caretaker,**user_data)
        return caretaker****

    class Meta:
        model = SellerProfile
        fields=('user','company_name','company_phone','company_zip','seller_type','seller_product')

    @receiver(post_save, sender=settings.AUTH_USER_MODEL)
    def create_auth_token(sender, instance=None, created=False, **kwargs):
        if created:
            Token.objects.create(user=instance) 

View.py

class SellerUsers(ModelViewSet):
    serializer_class = SellerProfileCreateSerializer
    queryset = SellerProfile.objects.all()

1 个答案:

答案 0 :(得分:0)

# insert your values seperately, so that you can see what are values that you inserting in the database table.

class SellerProfileCreateSerializer(serializers.ModelSerializer):
    user = UserCreateSerializer()

    def create(self, validated_data):
        caretaker = SellerProfile.objects.create(
            user_id = validated_data['user_id'],
            company_name = validated_data["company_name"],
            company_phone = validated_data["company_phone"],
            ....
            ....
        )
        return super(SellerProfileCreateSerializer, self).create(validated_data)