Django REST Framework -- is multiple nested serialization possible? - django

I would like to do something like the following:
models.py
class Container(models.Model):
size = models.CharField(max_length=20)
shape = models.CharField(max_length=20)
class Item(models.Model):
container = models.ForeignKey(Container)
name = models.CharField(max_length=20)
color = models.CharField(max_length=20)
class ItemSetting(models.Model):
item = models.ForeignKey(Item)
attribute_one = models.CharField(max_length=20)
attribute_two = models.CharField(max_length=20)
serializers.py
class ItemSettingSerializer(serializers.ModelSerializer):
class Meta:
model = ItemSetting
class ItemSerializer(serializers.ModelSerializer):
settings = ItemSettingSerializer(many=True)
class Meta:
model = Item
fields = ('name', 'color', 'settings')
class ContainerSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True)
class Meta:
model = Container
fields = ('size', 'shape', 'items')
When I do nesting of only one level (Container and Item) it works for me. But when I try to add another level of nesting with the ItemSetting, it throws an AttributeError and complains 'Item' object has no attribute 'settings'
What am I doing wrong?

Multiple nested serialization works for me. The only major difference is that I specify a related_name for the FK relationships. So try doing this:
class Item(models.Model):
container = models.ForeignKey(Container, related_name='items')
class ItemSetting(models.Model):
item = models.ForeignKey(Item, related_name='settings')
Hope this works for you.

Related

Is there anyway to create related entites within a post request with django rest framwork?

If I sound confused, it's because I am.
I'm unfamiliar with the django rest framework and I'm attempting to create a relatively simple Recipe-Managing app that allows you to automatically create your shopping list.
Motivations :
I know that DRF might not be needed and I could just use django, but the point of this app is to learn how to use the DRF.
The goal is to create a back with DRF and do some fancy shenanigans with a front framework afterward.
Problem:
I have a Recipe model which contains a ManyToMany field to Ingredient through RecipeIngredient. And I am a bit confused on how I should approach the RecipeSerializer.
So far it looks like that :
class RecipeSerializer(serializers.ModelSerializer):
class Meta:
model = Recipe
fields = ('id','name','ingredients','tags','prep_time','cook_time', 'servings', 'instructions')
But I feel like whenever I will want to create a Recipe, I'll have to fire a post request to create the Ingredients (if they do not exist yet), one to create the Instructions, one to create the Recipe and one to create the RecipeIngredients.
Question :
Is there a way to make one request containing the recipe and all sub fields (ingredient, recipeingredient, instruction) and to create all the entities ?
That would be handled by the create function of the RecipeSerializer I suppose.
Model:
class Tag(models.Model):
name = models.CharField(max_length=100, unique=True)
class Ingredient(models.Model):
name = models.CharField(max_length=100, unique=True)
class Recipe(models.Model):
name = models.CharField(max_length=100)
ingredients = models.ManyToManyField(Ingredient,through='RecipeIngredient')
tags = models.ManyToManyField(Tag, related_name='recipes')
prep_time = models.PositiveIntegerField()
cook_time = models.PositiveIntegerField()
servings = models.PositiveIntegerField()
class Instruction(models.Model):
number = models.PositiveIntegerField()
text = models.TextField()
recipe = models.ForeignKey(Recipe, related_name='instructions', on_delete = models.CASCADE)
class RecipeIngredient(models.Model):
ingredient = models.ForeignKey(Ingredient, on_delete = models.CASCADE)
recipe = models.ForeignKey(Recipe, on_delete = models.CASCADE)
quantity = models.PositiveIntegerField()
unit = models.CharField(max_length=30, null= False, blank=True)
Serializers:
class TagSerializer(serializers.ModelSerializer):
recipes = serializers.PrimaryKeyRelatedField(queryset = Recipe.objects.all(), many = True)
class Meta:
model = Tag
fields = ('id','name', 'recipes')
class InstructionSerializer(serializers.ModelSerializer):
class Meta:
model = Instruction
fields = ('id','number','text','recipe')
class IngredientSerializer(serializers.ModelSerializer):
class Meta:
model = Ingredient
fields = ('id','name')
class RecipeIngredientSerializer(serializers.ModelSerializer):
class Meta:
model = RecipeIngredient
fields = ('id','ingredient','recipe','quantity','unit')
class RecipeSerializer(serializers.ModelSerializer):
class Meta:
model = Recipe
fields = ('id','name','ingredients','tags','prep_time','cook_time', 'servings', 'instructions')
You can use nested serializers, You can change the RecipeSerializer as follows
class IngredientSerializer(serializers.ModelSerializer):
class Meta:
model = Ingredient
fields = '__all__'
class RecipeSerializer(serializers.ModelSerializer):
ingredients = IngredientSerializer(many=True)
class Meta:
model = Recipe
fields = ('id','name','ingredients','tags','prep_time','cook_time', 'servings', 'instructions')

Django Creating constraints on serializers

I'm trying to extend Django's Poll app. Each poll will have 2 choices and either an image for each choice or a color for each choice, but not both. My models look like this:
class Question(models.Model):
question = models.CharField()
created_at = models.DateTimeField(auto_now_add=True)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice = models.CharField(max_length=120)
vote_count = models.IntegerField(default=0)
class ChoiceImage(models.Model):
choice = models.OneToOneField(Choice, on_delete=models.CASCADE)
img = models.ImageField()
class ChoiceColor(models.Model):
choice = models.OneToOneField(Choice, on_delete=models.CASCADE)
color = models.CharField(max_length=7)
I have a View that looks like this:
class CreateQuestionView(CreateAPIView):
serializer_class = CreateQuestionSerializer
With the following serializers:
class ChoiceImageSerializer(serializers.ModelSerializer):
class Meta:
model = ChoiceImage
fields = ('image',)
class ChoiceColorSerializer(serializers.ModelSerializer):
class Meta:
model = ChoiceColor
fields = ('color',)
class ChoiceSerializer(serializers.ModelSerializer):
choice_image = ChoiceImageSerializer(many=False)
choice_color = ChoiceColorSerializer(many=False)
class Meta:
model = Choice
fields = ('choice', 'choice_image', 'choice_color')
class CreateQuestionSerializer(serializers.ModelSerializer):
choices = ChoiceSerializer(many=True)
class Meta:
model = Question
fields = ('question', 'choices')
How do I create this constraint on my serializers? Also Am I designing this efficiently or is there a better way to do this?

Learning DRF: Serialize models in a group

I'm new to both Django and DRF. I need to expose the following JSON via GET, but I seem to be having trouble getting each class to be a part of classes []. I belive I can use StringRelatedField for this, but the examples I have found don't seem to click with me. Thanks in advance!
expected output
{
"classes":[
{
"id":24,
"name":"Math 101",
"starts_on":"2016-08-09",
"ends_on":"2016-08-14",
"entries_url":"https://example.com/classes/24/classes.json"
},
{
"id":23,
"name":"English 101",
"starts_on":"2016-07-28",
"ends_on":"2016-07-30",
"entries_url":"https://example.com/classes/23/classes.json"
}
]
}
Here's what I've cobbled together. "nr" is just a throwaway variable as I don't actually want any data besides what comes from class.
model.py
class Class(models.Model):
id = models.CharField(max_length=10)
starts_on = models.DateTimeField(auto_now_add=False)
ends_on = models.DateTimeField(auto_now_add=False)
entries_url = models.CharField(max_length=150)
class Meta:
ordering = ('id',)
class Classes(models.Model):
nr = models.CharField(max_length=100)
serializers.py
class ClassesSerializer(serializers.ModelSerializer):
class = serializers.StringRelatedField(many=True)
class Meta:
model = Classes
fields = ('classes')
Your database needs to keep a relation between your tables. You can use a foreign key here:
class Class(models.Model):
id = models.CharField(max_length=10)
starts_on = models.DateTimeField(auto_now_add=False)
ends_on = models.DateTimeField(auto_now_add=False)
entries_url = models.CharField(max_length=150)
related_classes = models.ForeignKey('Classes', on_delete=models.CASCADE, related_name='classes')
class Meta:
ordering = ('id',)
class Classes(models.Model):
nr = models.CharField(max_length=100)
Then, your serializer becomes:
class ClassSerializer(serializers.ModelSerializer):
class Meta:
model = Class
fields = ['__all__']
class ClassesSerializer(serializers.ModelSerializer):
classes = ClassSerializer(many=True)
class Meta:
model = Classes
fields = ('classes',)
Also, I strongly discourage you to use class for your variables, since it clashes with the class keyword of Python.

How to set SlugRelated field to a field within an object field

I have the following models:
class Category(models.Model):
name = models.CharField()
... # fields omitted
class Prediction(models.Model):
conversation = models.ForeignKey(Conversation)
category = models.ForeignKey(Category)
... # fields omitted
class Conversation(models.Model):
sid = models.CharField()
... # fields omitted
Now I'm trying to create a model serializer for Category that would return me the following serialized object:
{
"name":"blah",
"conversations":[
"2af22188c5c97256", # This is the value of the sid field
"073aef6aad0883f8",
"5d3dc73fc8cf34be",
]
}
Here is what I have in my serializer:
class CategorySerializer(serializers.ModelSerializer):
conversations = serializers.SlugRelatedField(many=True,
read_only=True,
source="prediction_set",
slug_field='conversation.sid')
class Meta:
model = models.Class
fields = ('class_name', 'conversations')
However, this doesn't work because somehow django doesn't allow me to set slug_field to be a field that's within an object field. Any suggestions on how to accomplish this?
You are modelling a Many-to-Many relationship between Categorys and Conversations with a explicit table called Prediction. The django way of doing this would be to explicitly state the Many-to-Many on either side of the relationship and specify Prediction as the "through-model":
Shamelessly stolen example from this question:
class Category(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=255, blank=True,default=None)
desc = models.TextField(blank=True, null=True )
...
class Post(models.Model):
title = models.CharField(max_length=255)
pub_date = models.DateTimeField(editable=False,blank=True)
author = models.ForeignKey(User, null=True, blank=True)
categories = models.ManyToManyField(Category, blank=True, through='CatToPost')
...
class CatToPost(models.Model):
post = models.ForeignKey(Post)
category = models.ForeignKey(Category)
...
This shows a good way to set up the relationship.
Equally shamelessly stolen from the answer by #Amir Masnouri:
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ('name','slug')
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('id','{anything you want}','categories')
depth = 2
This shows a nice way of achieving the nested serialization behavior that you would like.

Django REST Framework - query limit on nested serializer?

I have a situation in which one table is related to another via a foreign key as follows:
models.py
class Container(models.Model):
size = models.CharField(max_length=20)
shape = models.CharField(max_length=20)
class Item(models.Model):
container = models.ForeignKey(Container, related_name='items')
name = models.CharField(max_length=20)
color = models.CharField(max_length=20)
serializers.py
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
class ContainerSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True)
class Meta:
model = Container
fields = ('size', 'shape', 'items')
This works fine, but my problem is that all the items in the container get serialized. I only want items with color='green' to be serialized.
class ContainerSerializer(serializers.ModelSerializer):
items = serializers.SerializerMethodField()
def get_items(self, obj):
query = Item.objects.filter(item_set__color='green')
serializer = ItemSerializer(query, many=True)
return serializer.data
class Meta:
model = Container
fields = ('size', 'shape', 'items')
Instead of changing how serializer works, a simplier way, its just filter you Container with green color items and them try to serialize it
You can do something like this:
container_objects = Container.objects.filter(id='your_container_id',item_set__color='green')
serialized_containers = YourContainerSerializer(data=container_objects)