添加附加字段数据以响应 Django 中的 post 方法
Add additional fields data in response to post method in Django
我需要为 POST 响应显示额外的字段数据。这个额外的字段数据不是插入或更新到数据库中,只是想得到响应。此附加数据来自另一个模型。我需要自定义响应 JSON 表示
型号
class Country(BaseModel):
name= models.CharField(null=True)
state= models.FloatField(null=True)
class Meta:
db_table = 'country'
class Tour(BaseModel):
name = models.ForeignKey(Country, on_delete=models.PROTECT)
country= models.FloatField(null=True)
class Meta:
db_table = 'tour'
class TourInter(BaseModel):
tour = models.ForeignKey(Tour, on_delete=models.PROTECT)
price= models.FloatField(null=True)
details= models.TextField(null=True, blank=True)
class Meta:
db_table = 'tourinternational'
Serializer.py
class TourInterCreateSerializer(serializers.ModelSerializer):
country = serializers.CharField(required=False,read_only=True)
class Meta:
model=TourInter
fields = ('id','tour','price','country')
def validate(self, attrs):
tour_id=attrs.get('tour').id
tourintid = TourInter.objects.filter(tour=tour_id)[0].id
countryobj = Tour.objects.get(id=tourid).country
country = countryobj.state
attrs.pop({'country': country})
attrs = super().validate(attrs)
return attrs
views.py
class TourInterViewSet(viewsets.ModelViewSet):
queryset = TourInter.objects.all()
def get_serializer_class(self):
if self.action == 'create' or self.action == 'update':
return TourInterCreateSerializer
return TourInterSerializer
def dispatch(self,request,*args,**kwargs):
response = super(TourInterViewSet, self).dispatch(request, *args, **kwargs)
data = {}
data= response.data
response.data = data
return response
post人工数据
要求:
{
tour_id: 1,
price: 10000
details: "Could be nil"
}
我需要一个 postman 回复,比如下面的国家名称
post 回复,这里国家没有插入数据库:
{
tour_id: 1,
price: 10000
details: "Could be nil",
country: "Country name from country model"#this field should be added in response
}
像这样更新序列化器
class TourInterCreateSerializer(serializers.ModelSerializer):
country = serializers.SerializerMethodField()
def get_country(self, instance):
# Get country from country model
return 'abc' # Write your own logic here
class Meta:
model=TripVisa
fields = ('id','tour','price','country')
def validate(self, attrs):
tour_id=attrs.get('tour').id
tourintid = TourInter.objects.filter(tour=tour_id)[0].id
countryobj = Tour.objects.get(id=tourid).country
country = countryobj.state
attrs.pop({'country': country})
attrs = super().validate(attrs)
return attrs
我需要为 POST 响应显示额外的字段数据。这个额外的字段数据不是插入或更新到数据库中,只是想得到响应。此附加数据来自另一个模型。我需要自定义响应 JSON 表示
型号
class Country(BaseModel):
name= models.CharField(null=True)
state= models.FloatField(null=True)
class Meta:
db_table = 'country'
class Tour(BaseModel):
name = models.ForeignKey(Country, on_delete=models.PROTECT)
country= models.FloatField(null=True)
class Meta:
db_table = 'tour'
class TourInter(BaseModel):
tour = models.ForeignKey(Tour, on_delete=models.PROTECT)
price= models.FloatField(null=True)
details= models.TextField(null=True, blank=True)
class Meta:
db_table = 'tourinternational'
Serializer.py
class TourInterCreateSerializer(serializers.ModelSerializer):
country = serializers.CharField(required=False,read_only=True)
class Meta:
model=TourInter
fields = ('id','tour','price','country')
def validate(self, attrs):
tour_id=attrs.get('tour').id
tourintid = TourInter.objects.filter(tour=tour_id)[0].id
countryobj = Tour.objects.get(id=tourid).country
country = countryobj.state
attrs.pop({'country': country})
attrs = super().validate(attrs)
return attrs
views.py
class TourInterViewSet(viewsets.ModelViewSet):
queryset = TourInter.objects.all()
def get_serializer_class(self):
if self.action == 'create' or self.action == 'update':
return TourInterCreateSerializer
return TourInterSerializer
def dispatch(self,request,*args,**kwargs):
response = super(TourInterViewSet, self).dispatch(request, *args, **kwargs)
data = {}
data= response.data
response.data = data
return response
post人工数据
要求:
{
tour_id: 1,
price: 10000
details: "Could be nil"
}
我需要一个 postman 回复,比如下面的国家名称 post 回复,这里国家没有插入数据库:
{
tour_id: 1,
price: 10000
details: "Could be nil",
country: "Country name from country model"#this field should be added in response
}
像这样更新序列化器
class TourInterCreateSerializer(serializers.ModelSerializer):
country = serializers.SerializerMethodField()
def get_country(self, instance):
# Get country from country model
return 'abc' # Write your own logic here
class Meta:
model=TripVisa
fields = ('id','tour','price','country')
def validate(self, attrs):
tour_id=attrs.get('tour').id
tourintid = TourInter.objects.filter(tour=tour_id)[0].id
countryobj = Tour.objects.get(id=tourid).country
country = countryobj.state
attrs.pop({'country': country})
attrs = super().validate(attrs)
return attrs