Answer the question
In order to leave comments, you need to log in
How to properly serialize an object?
How to properly serialize an object?
models.py
# -*- coding: utf-8 -*-
from django.db import models
class ingridient(models.Model):
name = models.CharField(verbose_name = u'Ингридиент',max_length=30, unique=True)
slug = models.SlugField(verbose_name = u'URL',primary_key=True, max_length=250, unique=True, default='SOME STRING')
def __unicode__(self):
return self.name
class recipe(models.Model):
name = models.CharField(verbose_name = u'Рецепт',max_length=30,unique=True)
ingridients = models.ManyToManyField(ingridient, verbose_name=u'Ингридиенты')
slug = models.SlugField(verbose_name = u'URL',primary_key=True, max_length=250, unique=True, default='SOME STRING')
def get_ing(obj):
return ','.join(obj.ingridients.values_list('name', flat=True))
get_ing.short_description = 'Ингридиенты'
def __unicode__(self):
return self.name
class RecipeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = recipe
#fields = ('url', 'name')
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
queryset = recipe.objects.all()
serializer_class = RecipeSerializer
url(r'^api/$', include('rest_framework.urls', namespace='rest_framework')),
urlpatterns = format_suffix_patterns(urlpatterns)
Answer the question
In order to leave comments, you need to log in
I would write like this:
from django.db import models
class Ingridient(models.Model):
name = models.CharField(
max_length=30,
unique=True,
db_index=True
)
class Meta:
db_table = 'ingridient'
ordering = [ 'name' ]
class Recipe(models.Model):
name = models.CharField(
max_length=30,
unique=True,
db_index=True
)
ingridients = models.ManyToManyField(
Ingridient
)
class Meta:
db_table = 'recipe'
ordering = [ 'name' ]
from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField
from .models import Ingridient, Recipe
class IngridientSerializer(ModelSerializer):
class Meta:
model = Ingridient
class RecipeReadListSerializer(ModelSerializer):
class Meta:
model = Recipe
exclude = ['ingridients']
class RecipeReadDetailSerializer(ModelSerializer):
ingridients = IngridientSerializer(
many=True
)
class Meta:
model = Recipe
class RecipeWriteSerializer(ModelSerializer):
ingridients = PrimaryKeyRelatedField(
queryset=Ingridient.objects.all(),
many=True,
default=[]
)
class Meta:
model = Recipe
from rest_framework.viewsets import ModelViewSet
from .serializers import IngridientSerializer, RecipeReadListSerializer, RecipeReadDetailSerializer, RecipeWriteSerializer
from .models import Ingridient, Recipe
from rest_framework.permissions import AllowAny
class IngridientViewSet(ModelViewSet):
queryset = Ingridient.objects.all()
serializer_class = IngridientSerializer
permission_classes = [
AllowAny
]
class Meta:
model = Ingridient
class RecipeViewSet(ModelViewSet):
queryset = Recipe.objects.all()
permission_classes = [
AllowAny
]
def get_serializer_class(self):
if self.request.method == 'GET':
if self.action == 'list':
return RecipeReadListSerializer
return RecipeReadDetailSerializer
return RecipeWriteSerializer
from rest_framework.routers import SimpleRouter
from .views import IngridientViewSet, RecipeViewSet
router = SimpleRouter()
router.register(r'ingridients', IngridientViewSet, base_name='ingridients')
router.register(r'recipies', RecipeViewSet, base_name='recipe')
urlpatterns = router.urls
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question