Reputation: 643
I am trying to provide a nested json file through Django Rest Framework. I have been reading up on different way to nest my serializers but I am not getting it to work.
I don't know how to create a modelserializer by myself, instead of directly pulling the data from my model...
My error:
AttributeError at /listings.json Got AttributeError when attempting to get a value for field
coordinates
on serializerTestSerializer
. The serializer field might be named incorrectly and not match any attribute or key on theTest
instance. Original exception text was: 'Test' object has no attribute 'coordinates'.
My serializer code:
from rest_framework import serializers
from rentlistings.models import Test
class coordinatesSerializer(serializers.ModelSerializer):
class Meta:
model = Test
fields = ('latitude', 'longitude')
class propertiesSerializer(serializers.ModelSerializer):
class Meta:
model = Test
fields = ('price', 'price', 'yields',
'num_floor', 'num_rooms', 'elevator', 'garage',
'balcony_size', 'garden_area', 'parking', 'terass',
'loggia', 'cellar', 'hash_id')
class TestSerializer(serializers.Serializer):
coordinates = coordinatesSerializer(many=True, read_only=True)
properties = propertiesSerializer(many=True, read_only=True)
class Meta:
model = Test
fields = ('coordinates', 'properties')
My views.py
from rentlistings.models import Test
from rentlistings.serializers import TestSerializer
from rest_framework import generics
# Create your views here.
class test_list(generics.ListCreateAPIView):
queryset = Test.objects.all()
serializer_class = TestSerializer´
After correcting with @aircraft comments, I don't get an error but nothing is included in the API json:
models.py
from django.db import models
# Create your models here.
class Test(models.Model):
latitude = models.FloatField(blank=True, null=True)
longitude = models.FloatField(blank=True, null=True)
hash_id = models.BigIntegerField(primary_key=True, blank=True)
price = models.BigIntegerField(blank=True, null=True)
floor = models.TextField(blank=True, null=True)
garden_area = models.FloatField(blank=True, null=True)
parking = models.TextField(blank=True, null=True)
terass = models.TextField(blank=True, null=True)
loggia = models.TextField(blank=True, null=True)
cellar = models.TextField(blank=True, null=True)
elevator = models.NullBooleanField()
garage = models.TextField(blank=True, null=True)
balcony_size = models.TextField(blank=True, null=True)
date = models.DateTimeField(blank=True, null=True)
last_seen = models.DateTimeField(blank=True, null=True)
num_floor = models.BigIntegerField(blank=True, null=True)
num_rooms = models.BigIntegerField(blank=True, null=True)
yields = models.FloatField(blank=True, null=True)
class Meta:
managed = False
db_table = 'test'
Upvotes: 0
Views: 4733
Reputation: 26914
In your TestSerializer
, you should add params like bellow:
class TestSerializer(serializers.Serializer):
coordinates = coordinatesSerializer(many=True, read_only=True)
properties = propertiesSerializer(many=True, read_only=True)
class Meta:
model = Test
fields = ('coordinates', 'properties')
Upvotes: 1