[英]Model object has no attribute HyperlinkedRelatedField
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. 我已经检查了几十个示例,并且我认为我做得正确,但是我收到此错误消息“ City”对象没有属性“ store”,请帮助,请参阅随附的序列化程序。
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. 默认情况下,DRF将通过模型中没有的
store
名称查看相关对象。
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"
. 无论是设置related_name“存储”或串行的使用源串行的域的参数源
store
,并设置为"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 如果您仍然不清楚,请查看教程http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/#adding-information-to-our-模型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.