我的django项目中有两个模型。 AdIfo和AdGame。 Adgame模型与Adjfo.in django管理面板具有OneToOne关系。我可以构建正确的Object AdGame模型。但是,当我尝试在序列化程序中进行操作时,错误是:IntegrityError
FOREIGN KEY constraint failed
Models.py:
class AdIfo(models.Model):
address = models.TextField(max_length=250)
price = MoneyField(max_digits=12, decimal_places=0,
default_currency="IRR")
phonenumber = models.CharField(validators=[phone_regex],
max_length=17, null=True, blank=True)
ad_status = models.BooleanField()
class AdGame(models.Model):
CONSOLE_GAME = (
('XBX', 'Xbox'),
('PS4', 'Play Station 4'),
('PC', 'Personal Computer'),
)
GAME_REGION = (
('1', 'USA'),
('2', 'Europe'),
('3', 'MiddleEast'),
('j', 'Japan'),
('4', 'Australia'),
('N', 'Unknown')
)
title = models.CharField(max_length=25)
game = models.ForeignKey(Game, on_delete=models.CASCADE)
console = models.CharField(max_length=3, choices=CONSOLE_GAME)
game_region = models.CharField(max_length=1, choices=GAME_REGION)
ad_info = models.OneToOneField(AdIfo, related_name='ad_info',
primary_key=True, on_delete=models.CASCADE)
description = models.TextField(max_length=500, blank=True)
owner = models.ForeignKey(User, related_name='ad_owner',
on_delete=models.CASCADE)
image = StdImageField(upload_to=UploadToClassNameDirUUID(),
blank=True, variations={
'large': (800, 600),
'medium': (600, 400),
'small': (300, 200),
'thumbnail': (100, 100, True),
})
publish_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
serializers.py
class AdInfoSerializer(serializers.ModelSerializer):
"""ad information serialization"""
phonenumber = serializers.IntegerField()
class Meta:ad_info=adgame,
model = AdIfo
fields = "__all__"
class AdGameSerilizer(serializers.ModelSerializer):
''' Ad model serialization '''
ad_info = AdInfoSerializer(required=True)
console = serializers.ChoiceField(choices=AdGame.CONSOLE_GAME)
game_region = serializers.ChoiceField(choices=AdGame.GAME_REGION)
owner = serializers.PrimaryKeyRelatedField(read_only=True,
default=serializers.CurrentUserDefault())
image = serializers.ImageField()
class Meta:
model = AdGame
fields = [
'ad_info',
'title',
'game',
'console',
'game_region',
'image',
'owner',
'description',
'publish_date',
]
def create(self, validated_data):
info_data = validated_data.pop('ad_info')
adgame = AdGame.objects.create(**validated_data)
AdIfo.objects.create(ad_info=adgame, **info_data)
adgame.save()
return adgame
def update(self, instance, validated_data):
info_data = validated_data.pop('ad_info')
instance.ad_info.address = info_data.get('address',
instance.ad_info.address)
instance.ad_info.price = info_data.get('price',
instance.ad_info.price)
instance.ad_info.phonenumber = info_data.get('phonenumber',
inad_info=adgame,stance.ad_info.phonenumber)ad_info=adgame,
instance.ad_info.ad_status = info_data.get('ad_status',
instance.ad_info.ad_status)
instance.title = validated_data.get('title', instance.title)
instance.image = validated_data.get('image', instance.image)
instance.console = validated_data.get('console',
instance.console)
instance.game = validated_data.getad_info=adgame,('game', instance.game)
instance.game_region = validated_data.get('game_region',
instance.game_region)
instance.description = validated_data.get('description',
instance.description)
instance.owner = validated_data.get('owner', instance.owner)
info = instance.ad_info
instance.save()ad_info=adgame,
info.save()
return instance
我知道我的问题是在django rest框架中你必须处理create()
。必须首先创建模型AdIfo,然后创建AdGame.I尝试将Adgame的AdIame ID作为kwargs传递,但我的知识还不够。我意识到AdIfo没有将adInfoSerializer中的id传递给AdGameSerilizer。但是我做了测试AdInfoSerializer的视图。一切都很好,id字段将包含在我的结果中。