Introduction to Serializers in the Django REST Framework

Moeedlodhi
Geek Culture
Published in
7 min readJul 25, 2021

--

Everything I learned about Serializers in one of my recent projects

Photo by Shahadat Rahman on Unsplash

Probably one of the most important features in Django, Serializers are a massive help when trying to present data in a format for the Front-end to understand. JSON is the name of the game and if you know how to quickly “jsonify” the Front-ends requested data, you just leveled up your speed of development. So let's get started.

For this article, I will be going through an example that will summarize all that I learned.

So let's say, I have a model which looks like this

class Items_list(models.Model):
user = models.ForeignKey(User, related_name="dairy_list", on_delete=models.CASCADE)
category = models.CharField(max_length=264,null=True,blank=True)

class Meta:
unique_together = ['user', 'category']

def __str__(self):
return (str(self.user) + ' ' + (self.category))


class items(models.Model):
category = models.CharField(max_length=264,null=True,blank=True)
item = models.CharField(max_length=264)
items_list = models.ForeignKey(
Items_list, related_name="list", on_delete=models.CASCADE
)

quantity=models.FloatField(null=True,blank=True)

class Meta:
unique_together = ['category', 'item','items_list']

def __str__(self):
return…

--

--