18👍
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.
16👍
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'
)
7👍
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')
- Issues with queryset and slicing
- Pass list of fields to QuerySet.values( )
- How to dynamically add EC2 ip addresses to Django ALLOWED_HOSTS
0👍
class RepairListCreateView(APIView):
"""
List all repairs, or create a new repair.
"""
serializer_class = RepairSerializer
permission_classes = [permissions.AllowAny]
def get(self, request, format=None):
"""
Return a list of all repairs.
"""
repairs = Repair.objects.all()
serializer = self.serializer_class(repairs, many=True)
return Response(serializer.data)
def post(self, request, format=None):
"""
Create a new repair.
"""
serializer = self.serializer_class(data=request.data)
print(request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class Repair(models.Model):
"""
Model for a repair
"""
STATUS_CHOICES = (
('P', 'Pending'),
('C', 'Completed'),
('A', 'Approved'),
('R', 'Rejected'),
)
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
description = models.TextField()
status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='P')
is_deleted = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
"""
Meta definition for Repair
"""
db_table = 'Repairs'
verbose_name = 'Repair'
verbose_name_plural = 'Repairs'
def __str__(self):
return self.title
The field ‘user_id’ was declared on serializer RepairSerializer, but has not been included in the ‘fields’ option
- Django Channels VS Django 3.0 / 3.1?
- Django how to reconnect after DatabaseError: query timeout
- Add a prefix to URL patterns
- What is the right way to use angular2 http requests with Django CSRF protection?