John Kaff
John Kaff

Reputation: 2075

How to get serialize the model in django REST

I have the django Model

class User(models.Model):
    def serialize(self):
        serializer = UserSerializer(data=self)
        if serializer.is_valid():

           return serializer.data

basically i want to get the serialized version of current model instance

obj = User.objects.get(pk=1)
obj.serialize()

but i get error that serializers expects Dict u gave User

Upvotes: 2

Views: 847

Answers (1)

Gocht
Gocht

Reputation: 10256

Do not pass data param, in this case, you just need to pass the object:

def serialize(self):
    serializer = UserSerializer(self)
    if serializer.is_valid():

       return serializer.data

As you can see here in Serializing Objects section

Upvotes: 3

Related Questions