我检查了几十个示例,我认为我这样做是正确的,但是我收到此错误消息“城市”对象没有属性“商店”,请帮助,请参阅随附的序列化程序。
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",
]
模型.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)