Model.py
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)
So in my serializer class i am comparing labels from Android model and server model in (def get_returnlabel),and i want to return this label back to my android app.Any suggestons on how to do it.On my android app I am using async http.
Serializer.py
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') (<--returnlabel back to android app)
#Compare label from Server and Android
def get_return_label(self, obj):
queryset = Server.objects.filter( labelServer=obj.label)
queryset_serializer = FoodSerializers( queryset, many=True, read_only=True)
return queryset_serializer.data
Views.py
class FoodViewSet(viewsets.ModelViewSet):
queryset = Server.objects.all()
serializer_class =FoodSerializers
class Androids(viewsets.ModelViewSet):
queryset =Android.objects.all()
serializer_class = AndroidSerializers
Assuming I've understood your question correctly, and that you're basically asking how you would return the output from
get_return_label()back to your Android client, then you should just be able to create an extra action in your viewset for that. You'd need to relocateget_return_label()to the viewset first.For example:
To access it, you'd use the existing URL of
FoodViewSetappended with/get_return_label/The alternative is to create a dedicated view end point:
And map that in your
urls.py: