2

i want to add Status and place the output of serializer into DATA:

My Model:

class UserDetails(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    user_group_id = models.ForeignKey(UserGroup, on_delete=models.CASCADE)
    admin_photo = models.ImageField(upload_to='user_image',blank=True)

My Serializer:

class UserDetailSerializer(serializers.ModelSerializer):
   user = serializers.StringRelatedField(many=False)
   user_group_id = serializers.StringRelatedField(many=False)
   class Meta:
      model = UserDetails
      fields = [
         'user',
         'user_group_id',
         'admin_photo',
      ]
      depth = 1

  def to_representation(self, instance):
      data = super(UserDetailSerializer, self).to_representation(instance)
      return {
            'STATUS' : 'SUCCESS',
            'DATA' : data 
        }

the response i got is this:

[
    {
        "DATA": {
            "user": "user1",
            "user_group_id": "Super Admin",
            "admin_photo": "http://www.someurl.com/media/22-black-wallpaper.jpg"
        },
        "STATUS": "SUCCESS"
    },
    {
        "DATA": {
            "user": "user2",
            "user_group_id": "Admin",
            "admin_photo": "http://www.someurl.com/media/fox.jpg"
        },
        "STATUS": "SUCCESS"
    }
]

but i want response like this, status are basic text, and i want to put the result from Class meta into DATA

{
    "STATUS": "SUCCESS",
    "DATA": [
       {
          "user": "user1",
          "user_group_id": "Super Admin",
          "admin_photo": "http://www.someurl.com/media/22-black- 
                          wallpaper.jpg"
       },
       {
          "user": "user2",
          "user_group_id": "Admin",
          "admin_photo": "http://www.someurl.com/media/fox.jpg"
       }
     ]
 }

edited View just using listapiview and use serializer class :

class UserListView(generics.ListAPIView):
    lookup_vield = 'id'
    serializer_class = UserDetailSerializer

    def get_queryset(self):
       return UserDetails.objects.all()
8
  • First, what do you mean by STATUS? Will there be a chance to have multiple Status? Commented Oct 2, 2018 at 5:15
  • moreover, pls do add your curresponding view also Commented Oct 2, 2018 at 5:16
  • @JPG Its just Success or Failed Commented Oct 2, 2018 at 5:26
  • Will there be a chance to have multiple Status ina single response? Commented Oct 2, 2018 at 5:29
  • @JPG no iust single STATUS, Success or Failed Commented Oct 2, 2018 at 5:33

1 Answer 1

2

You can't do it in serializer level, but it's possible to do in view level by overriding the list(...) method of UserListView

Step 1. Remove to_representation method from UserDetailSerializer serializer
Step 2. Overriding the list() method of view as

class UserListView(generics.ListAPIView):
    lookup_vield = 'id'
    serializer_class = UserDetailSerializer

    def get_queryset(self):
        return UserDetails.objects.all()

    def list(self, request, *args, **kwargs):
        res = super(UserListView, self).list(request, *args, **kwargs)
        res.data = {"STATUS": "SUCCESS", "DATA": res.data}
        return res
Sign up to request clarification or add additional context in comments.

3 Comments

if you dont mind helping me once more,how can i implement this in generics.RetriveAPIView because when i use .retreive it doesnt work like .list example: res = super(UserListView, self).retreive(request, *args, **kwargs) res.data = {"STATUS": "SUCCESS", "DATA": res.data} return res
You have to override retrive(...) method instead of list(..) method
great it works, thanks again looks like im just wrong typing retreive lol

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.