Models.py 这是我的模型类,其中有2个表。每个表上都有一个称为label的字段。我的问题是我将如何比较2个标签并将值返回到新字段(returnlabel)
class Server(models.Model):
label = models.TextField(max_length=200,null=True) #compare this
upload1 = models.FileField(null=True, blank=True)
Image1 = models.TextField(upload1, null=True)
class Android(models.Model):
label=models.TextField(max_length=200,null=True) #with this
imagestring=models.TextField(null=True,blank=True)
returnlabel=models.TextField(null=True,blank=True) #return to this
序列化器类
class FoodSerializers(serializers.HyperlinkedModelSerializer):
class Meta:
model=Server
fields=('url','label','Image1','upload1')
class AndroidSerializers(serializers.ModelSerializer):
class Meta:
model = Android
fields = ('label', 'imagestring', 'returnlabel')
观看次数
class FoodViewSet(viewsets.ModelViewSet):
queryset = Server.objects.all()
serializer_class =FoodSerializers
class Androids(viewsets.ModelViewSet):
queryset =Android.objects.all()
serializer_class = AndroidSerializers
答案 0 :(得分:0)
您可以尝试使用SerializerMethodField。像这样尝试:
class AndroidSerializers(serializers.ModelSerializer):
return_label = serializers.SerializerMethodField()
class Meta:
model = Android
fields = ('label', 'imagestring', 'return_label')
def get_return_label(self, obj):
queryset = Server.objects.filter(
label=obj.label)
queryset_serializer = FoodSerializers(
queryset, many=True, read_only=True)
return queryset_serializer.data