0

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
15
  • In get_return_label(self, obj) where would obj come from? Would this be looked up from the incoming request? Commented Dec 1, 2018 at 10:43
  • Get_return_label line 2 .labelserver=obj.label so yes..please help me out here really desperatr Commented Dec 1, 2018 at 11:48
  • Ok so to clarify, you're essentially wanting to expose get_return_label() so that your Android application can retrieve the data from it remotely? Commented Dec 1, 2018 at 12:51
  • yes.Can u help me out? Commented Dec 1, 2018 at 13:49
  • I'll try. So when your Android app makes a request to get_return_label(), what parameters will it send? Perhaps you could post that code if you have written it. Commented Dec 1, 2018 at 14:16

1 Answer 1

0

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 relocate get_return_label() to the viewset first.

For example:

from rest_framework.response import Response

class FoodViewSet(viewsets.ModelViewSet):
    queryset = Server.objects.all()
    serializer_class = FoodSerializers

    @action(detail=False, methods=['post'])
    def get_return_label(self, request):
        obj = request.data  # The RecognizingFood object posted by the client

        queryset = Server.objects.filter(label=obj['label'])
        queryset_serializer = FoodSerializers(queryset, many=True, read_only=True)      
        return Response(queryset_serializer.data)

To access it, you'd use the existing URL of FoodViewSet appended with /get_return_label/

The alternative is to create a dedicated view end point:

from rest_framework import views

class GetReturnLabelView(views.APIView):

    def post(self, request):
        obj = request.data  # The RecognizingFood object posted by the client

        queryset = Server.objects.filter(label=obj['label'])
        queryset_serializer = FoodSerializers(queryset, many=True, read_only=True)      
        return Response(queryset_serializer.data)

    def get(self, request):
        queryset = Server.objects.filter(label=request.data['label'])
        queryset_serializer = FoodSerializers(queryset, many=True, read_only=True)      
        return Response(queryset_serializer.data)

And map that in your urls.py:

path(r'get_return_label', views.GetReturnLabelView.as_view())
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks but what changes would i need to make on my android code to see wether i am getting the values back
Both approaches assume your android client is making a POST request (from what I could establish from looking at your code). If you want to make a GET request, you'll need to update the methods list in approach 1, and add a get() function in approach 2. Your existing LOG statements in the onSuccess() callback should print out the values of the response you get back. Just make sure that your logging system is switched to log at the correct level (e.g. DEBUG) so that you actually see these messages.
but what do i put in get method?
can u help me here as well..so sorry
On mobile now but will look again later.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.