rest framework 序列化程序在 POST 请求时失败,模型在 OneToMany 关系中
rest framework serializer fails on POST request with models in OneToMany Relationship
我有两个模型
公寓模型
class Apartment(models.Model):
owner = models.ForeignKey(LandOwner, on_delete=models.CASCADE )
property_size = models.CharField(max_length = 255, null =False, blank = False)
location = models.CharField(max_length=255, blank=False, null=False )
name = models.CharField(max_length=255, blank=False, null=False )
town = models.CharField(max_length=255, blank=False, null=False )
category = models.ManyToManyField(HouseCategory)
no_of_house = models.IntegerField(blank=False, null=False )
year = models.IntegerField(blank=False, null=False )
房屋模型
class House(models.Model):
category = models.ForeignKey(HouseCategory, on_delete = models.CASCADE)
apartment = models.ForeignKey(Apartment, on_delete=models.CASCADE )
cost = models.CharField(max_length = 30, null=False, blank = False)
description = models.TextField(null = True, blank = True)
features = models.ManyToManyField(HouseFeatures)
occupied = models.IntegerField()
total = models.IntegerField()
他们的序列化器
ApartmentSerializer
class PropertySerializer(ModelSerializer):
class Meta:
model = models.Apartment
fields = '__all__'
HouseSerializer
class HouseSerializer(ModelSerializer):
class Meta:
model = models.House
fields = [
'description',
'apartment',
'category',
'occupied',
'cost',
'total',
'features',
]
depth = 1
对以下视图的 Get 请求
class Listing(ListCreateAPIView):
permission_classes = [ permissions.AllowAny ]
# permission_classes = [ permissions.IsAuthenticatedOrReadOnly ]
serializer_class = serializers.HouseSerializer
queryset = House.objects.all()
我得到这个JSON,这是我设计的输出
{
"description": "",
"apartment": {
"id": 1,
"property_size": "30Ha",
"location": "Nairobi",
"name": "Real Estate Apartment",
"town": "Nairobi",
"no_of_house": 4,
"year": 2019,
"owner": 1,
"category": [
1
]
},
"category": {
"id": 1,
"name": "Single Room"
},
"occupied": 2,
"cost": "40000",
"total": 5,
"features": [
{
"id": 1,
"name": "Source of Water"
},
{
"id": 2,
"name": "wifi availability"
},
{
"id": 3,
"name": "Swimming pool available"
}
]
}
问题是当我向同一个视图发送 POST 请求时,我收到“NOT NULL 约束失败:House.apartment_id”。与 POST 请求一起发送的 JSON 是
{
"apartment": 10,
"cost": "40000",
"description": "",
"occupied": 2,
"total": 5,
"category": 1,
"features": [
1,
2,
3
]
}
我希望视图对 post 请求做的是用给定的信息保存一个新房子,并将相关的现场公寓设置为我用 JSON 有效负载发送的 id
像这样更改序列化程序:
class HouseSerializer(ModelSerializer):
apartment = PropertySerializer(read_only=True, many=True)
class Meta:
model = models.House
fields = [
'description',
'apartment',
'category',
'occupied',
'cost',
'total',
'features',
]
我找到了这个问题的解决方案,在 属性 序列化器上,我将方法 def to_reperesentation(self, instance)
覆盖到这个
def to_representation(self, instance):
self.fields['owner'] = LandOwnerSerializer(read_only = True)
self.fields['category'] = HouseCategorySerializer(read_only = True, many = True)
return super(PropertySerializer, self).to_representation(instance)
并且在 HouseSerializer 中我也做了同样的(覆盖 to_representation)方法并添加了一些修改看起来像这样
def to_representation(self, instance):
self.fields['apartment'] = PropertySerializer(read_only = True, )
return super(HouseSerializer, self).to_representation(instance)
现在它工作得很好,添加房子只需要目标公寓的 Id,而获取则可以获取公寓的详细信息。感谢大家的参与。
我有两个模型
公寓模型
class Apartment(models.Model):
owner = models.ForeignKey(LandOwner, on_delete=models.CASCADE )
property_size = models.CharField(max_length = 255, null =False, blank = False)
location = models.CharField(max_length=255, blank=False, null=False )
name = models.CharField(max_length=255, blank=False, null=False )
town = models.CharField(max_length=255, blank=False, null=False )
category = models.ManyToManyField(HouseCategory)
no_of_house = models.IntegerField(blank=False, null=False )
year = models.IntegerField(blank=False, null=False )
房屋模型
class House(models.Model):
category = models.ForeignKey(HouseCategory, on_delete = models.CASCADE)
apartment = models.ForeignKey(Apartment, on_delete=models.CASCADE )
cost = models.CharField(max_length = 30, null=False, blank = False)
description = models.TextField(null = True, blank = True)
features = models.ManyToManyField(HouseFeatures)
occupied = models.IntegerField()
total = models.IntegerField()
他们的序列化器 ApartmentSerializer
class PropertySerializer(ModelSerializer):
class Meta:
model = models.Apartment
fields = '__all__'
HouseSerializer
class HouseSerializer(ModelSerializer):
class Meta:
model = models.House
fields = [
'description',
'apartment',
'category',
'occupied',
'cost',
'total',
'features',
]
depth = 1
对以下视图的 Get 请求
class Listing(ListCreateAPIView):
permission_classes = [ permissions.AllowAny ]
# permission_classes = [ permissions.IsAuthenticatedOrReadOnly ]
serializer_class = serializers.HouseSerializer
queryset = House.objects.all()
我得到这个JSON,这是我设计的输出
{
"description": "",
"apartment": {
"id": 1,
"property_size": "30Ha",
"location": "Nairobi",
"name": "Real Estate Apartment",
"town": "Nairobi",
"no_of_house": 4,
"year": 2019,
"owner": 1,
"category": [
1
]
},
"category": {
"id": 1,
"name": "Single Room"
},
"occupied": 2,
"cost": "40000",
"total": 5,
"features": [
{
"id": 1,
"name": "Source of Water"
},
{
"id": 2,
"name": "wifi availability"
},
{
"id": 3,
"name": "Swimming pool available"
}
]
}
问题是当我向同一个视图发送 POST 请求时,我收到“NOT NULL 约束失败:House.apartment_id”。与 POST 请求一起发送的 JSON 是
{
"apartment": 10,
"cost": "40000",
"description": "",
"occupied": 2,
"total": 5,
"category": 1,
"features": [
1,
2,
3
]
}
我希望视图对 post 请求做的是用给定的信息保存一个新房子,并将相关的现场公寓设置为我用 JSON 有效负载发送的 id
像这样更改序列化程序:
class HouseSerializer(ModelSerializer):
apartment = PropertySerializer(read_only=True, many=True)
class Meta:
model = models.House
fields = [
'description',
'apartment',
'category',
'occupied',
'cost',
'total',
'features',
]
我找到了这个问题的解决方案,在 属性 序列化器上,我将方法 def to_reperesentation(self, instance)
覆盖到这个
def to_representation(self, instance):
self.fields['owner'] = LandOwnerSerializer(read_only = True)
self.fields['category'] = HouseCategorySerializer(read_only = True, many = True)
return super(PropertySerializer, self).to_representation(instance)
并且在 HouseSerializer 中我也做了同样的(覆盖 to_representation)方法并添加了一些修改看起来像这样
def to_representation(self, instance):
self.fields['apartment'] = PropertySerializer(read_only = True, )
return super(HouseSerializer, self).to_representation(instance)
现在它工作得很好,添加房子只需要目标公寓的 Id,而获取则可以获取公寓的详细信息。感谢大家的参与。