i'm new python, , guess i'm serializing incorrectly.
authors/models.py:
django.db import models django.contrib.auth.models import user # create models here. class author(models.model): name = models.charfield(max_length=100) def __str__(self): return self.name class book(models.model): auto_increment_id = models.autofield(primary_key=true) name = models.charfield('book name', max_length=100) author = models.foreignkey(author, blank=false, null=false, related_name='book_author') contents = models.textfield('contents', blank=false, null=false) def __str__(self): return self.name
authors/views.py
from authors.models import book, author authors.serializers import bookserializer, authorserializer django.shortcuts import render rest_framework import generics class listcreatebooks(generics.listcreateapiview): queryset = book.objects.all() serializer_class = bookserializer class listcreateauthor(generics.listcreateapiview): queryset = author.objects.all() serializer_class = authorserializer
authors/serializers.py
from authors.models import book, author rest_framework import serializers class bookserializer(serializers.modelserializer): class meta: model = book fields = ('name', 'author') class authorserializer(serializers.modelserializer): class meta: model = author book_author = 'name'
i'm new django, tried many things, in views.py i've added class called authorserializer
importing corresponding class created in serializers, realized have no clue how add listcreateauthor
to:
url(r'^api-auth/', listcreatebooks.as_view(), name='list_books')
i've added parameter listcreateauthor.as_view()
gave me immediate error (and didn't make sense) going wrong way here , how can solve this?
edit: @abdulafaja gave partial answer, did solve read, after checking post insert - gives error create or update.
in django rest_framework's documentation (link provided @abdulafaja) give 1 example nested serializer, , it's 1 many relationship (album->tracks) mine 1 one (book->author) have no idea how serilize nested feature. api needs give frontend crud features.
thanks.
edited serializers.py abdullah:
from authors.models import book, author rest_framework import serializers class authorserializer(serializers.modelserializer): class meta: model = author book_author = 'name' class bookserializer(serializers.modelserializer): author = authorserializer(many=false) class meta: model = book fields = ('name', 'author') def create(self, validated_data): author, _ = author.objects.get_or_create(name=validated_data.get('author').get('name')) return book.objects.create(name=validated_data.get('name'), author=author) def update(self, instance, validated_data): author = validated_data.get('author') if author: instance.author = author.objects.get_or_create(name=author.get('name')) instance.name = validated_data.get('name', instance.name) instance.save() return instance
for book model author id of field in database, that's why it's return integer field. try add author field in book serializer
class bookserializer(serializers.modelserializer): author = authorserializer(read_only=true) class meta: model = book fields = ('name', 'author')
this nestedserializer, read-only default. on it's drf doc site link mentioned
need create create() and/or update() methods in order explicitly specify how child relationships should saved.
so bookserializer needs this
class bookserializer(serializers.modelserializer): author = authorserializer(read_only=true) class meta: model = book fields = ('name', 'author') def create(self, validated_data): # create new object def update(self, instance, validated_data): # update existing instance
both create
, update
methods need save object database , return it. there called when save bookserializer class instance. difference between them create
called when create new instance of book object
serializer = bookserializer(data=data)
and update
called if passed existing instance of book object when instantiating serializer class
serializer = bookserializer(book, data=data)
more information can find here
edit: if want create instance nestedserializer should not read field.
class bookserializer(serializers.modelserializer): author = authorserializer(many=false) class meta: model = book fields = ('name', 'author') def create(self, validated_data): author, _ = author.objects.get_or_create(name=validated_data.get('author').get('name')) return book.objects.create(name=validated_data.get('name'), author=author) def update(self, instance, validated_data): author = validated_data.get('author') if author: instance.author = author.objects.get_or_create(name=author.get('name')) instance.name = validated_data.get('name', instance.name) instance.save() return instance
Comments
Post a Comment