Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertionError: The field ' ' was declared on serializer ' ', but has not been included in the 'fields' option

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
like image 354
user3395936 Avatar asked Mar 31 '16 14:03

user3395936


3 Answers

The answers above already fix this specific issue. For me, I saw this error for entirely different reason, I was missing a comma in fields list after one of the attribute id, so I got the error regarding name.

class SomeSerializer(serializers.ModelSerializer):
    class Meta:
        model = SomeModel
        fields = (
                  'id' # missed a comma here
                  'name'
                 )
like image 124
Ash Singh Avatar answered Oct 29 '22 20:10

Ash Singh


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.

like image 24
rnevius Avatar answered Oct 29 '22 20:10

rnevius


Whatever field you will define in Serializer, you need to put it in the meta class fields. If you don't mention you will get the error.

builtins.AssertionError AssertionError: The field 'abc' was declared on serializer ABCSerializer, but has not been included in the 'fields' option.

So in your case you have defined doctor field in serializer so you meta fields should have this doctor field. It is case-sensitive. So you will have to use doctor instead of Doctor.

class AnimalSerialiser(serializers.HyperlinkedModelSerializer):
doctor = DoctorSerealiser()


class Meta:
    model = Animal
    fields = ('id' , 'name' , 'gender' , 'breed' , 'adoption' , 'vaccines', 'doctor')
like image 20
Umar Asghar Avatar answered Oct 29 '22 20:10

Umar Asghar