-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2c4bb3
commit 410089d
Showing
6 changed files
with
99 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 3.2.21 on 2023-09-20 14:03 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0002_recipe'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Tag', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=255)), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='recipe', | ||
name='tag', | ||
field=models.ManyToManyField(to='core.Tag'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
from rest_framework import serializers | ||
from core.models import Recipe | ||
from core.models import Recipe,Tag | ||
|
||
|
||
class TagSerialzer(serializers.ModelSerializer): | ||
class Meta: | ||
model=Tag | ||
fields=['id','name'] | ||
read_only_fields=['id'] | ||
class RecipeSerializer(serializers.ModelSerializer): | ||
tags = TagSerialzer(many=True,required=False) | ||
class Meta: | ||
model=Recipe | ||
fields=('id','title','price','link') | ||
read_only_fields=('id',) | ||
fields=['id','title','time_minutes','price','link','tags'] | ||
read_only_fields=['id'] | ||
def _get_or_create_tags(self,tags,recipe): | ||
auth_user=self.context['request'].user | ||
for tag in tags: | ||
tag_obj,created=Tag.objects.get_or_create(user=auth_user,**tag) | ||
recipe.tags.add(tag_obj) | ||
|
||
class RecipeDetailSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model=Recipe | ||
fields=('id','title','description','time_minutes','price','link') | ||
def create(self, validated_data): | ||
tags=validated_data.pop('tags',[]) | ||
recipe=Recipe.objects.create(**validated_data) | ||
self._get_or_create_tags(tags,recipe) | ||
return recipe | ||
def update(self,instance,validated_data): | ||
tags= validated_data.pop('tags',None) | ||
if tags is not None: | ||
instance.tags.clear() | ||
self._get_or_create_tags(tags,instance) | ||
for attr,value in validated_data.items(): | ||
setattr(instance,attr,value) | ||
instance.save() | ||
return instance | ||
|
||
|
||
|
||
class RecipeDetailSerializer(RecipeSerializer): | ||
"""Serializer for recipe detail view.""" | ||
|
||
class Meta(RecipeSerializer.Meta): | ||
fields = RecipeSerializer.Meta.fields + ['description'] | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters