Issue
I am using 'Django Rest Framework' and I am trying to build a RestfulAPI. However I get the above error when I run my app : AssertionError: The field 'doctor' was declared on serializer AnimalSerialiser, but has not been included in the 'fields' option.
I am not sure what fields
are and therefore can't track down the issue.
My models.py :
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Doctor(models.Model):
id= models.CharField(max_length=10, primary_key=True, unique=True)
name = models.CharField(max_length=20)
def __unicode__(self):
return self.id
class Animal(models.Model):
id = models.CharField(max_length=10, primary_key=True, unique=True)
name = models.CharField(max_length=200)
gender = models.CharField(max_length=10)
breed = models.CharField(max_length=200)
adoption = models.CharField(max_length=10)
vaccines = models.CharField(max_length=20)
doctor = models.ForeignKey(Doctor, null=True)
My serialisers.py :
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from models import Animal, Doctor
class DoctorSerealiser(serializers.HyperlinkedModelSerializer):
class Meta:
model = Doctor
fields = ('id' , 'name')
class AnimalSerialiser(serializers.HyperlinkedModelSerializer):
doctor = DoctorSerealiser()
class Meta:
model = Animal
fields = ('id' , 'name' , 'gender' , 'breed' , 'adoption' , 'vaccines', 'Doctor')
My views.py :
from django.shortcuts import render
# Create your views here.
from django.contrib.auth.models import User, Group
from rest_framework import viewsets, generics
from cw.myStart.models import Animal
from cw.myStart.serializers import AnimalSerialiser, DoctorSerealiser
from models import Animal, Doctor
class AnimalList(viewsets.ModelViewSet):
queryset = Animal.objects.all()
serializer_class = AnimalSerialiser
class DoctorDetail(viewsets.ModelViewSet):
queryset = Doctor.objects.all()
serializer_class = DoctorSerealiser
Solution
You need to modify your doctor
field name to be the proper case:
fields = ('id' , 'name' , 'gender' , 'breed' , 'adoption' , 'vaccines', 'doctor')
Doctor
is currently, incorrectly uppercase.
Answered By - rnevius
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.