I've checked dozens of examples and I think I am doing it the right way, however I am getting this error message 'City' object has no attribute 'store', Please help, see my serializer enclosed.
class CitySerializer(serializers.HyperlinkedModelSerializer):
store = serializers.HyperlinkedRelatedField(view_name = 'store:listStoreByCity',read_only=True)
class Meta:
model = City
read_only_fields = ['location']
fields = [
"city",
"latitude",
"longitude",
"store",
"state",
"img",
"location",
]
Models.py
from django.contrib.gis.db import models
from localflavor.us.us_states import STATE_CHOICES
class City(models.Model):
city = models.CharField(max_length=120)
latitude = models.CharField(blank=True, max_length=11, default=0)
longitude = models.CharField(blank=True, max_length=11, default=0)
state = models.CharField(max_length=2, choices=STATE_CHOICES, null=True, blank=True)
img = models.ImageField(upload_to='img', blank=True)
location = models.PointField(null=True, blank=True)
def __str__(self):
return str(self.city)
By default DRF will look at a related objects through the store
name which you don't have in your model.
Either set the related_name to "store" or use the source serializer's field's argument source in the serializer's store
and set it to "store_set"
.
If this is still unclear to you, have a look on these case described in tutorial http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/#adding-information-to-our-model
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.