[英]'ForeignKey' object has no attribute
When I try to make migrations for the following models: 当我尝试为以下模型进行迁移时:
class Location(models.Model):
name = models.CharField(max_length=200)
latitude = models.FloatField
longitude = models.FloatField
altitude = models.IntegerField
def _str_(self):
return self.name
def getLatitude(self):
return self.latitude
def getLongitude(self):
return self.longitude
class Distance(models.Model):
placeA = models.ForeignKey(Location, on_delete=models.CASCADE)
placeB = models.ForeignKey(Location, on_delete=models.CASCADE)
placeAlatitude = placeA.getLatitude()
placeBlatitude = placeB.getLatitude()
placeAlongitude = Location.objects.select_related().get(placeA.longitude)
placeBlongitude = Location.objects.select_related().get(placeB.longitude)
distance = models.FloatField(default = 0.0)
I am getting the error: 我收到错误消息:
'ForeignKey' object has no attribute 'getLatitude'
“ ForeignKey”对象没有属性“ getLatitude”
I get a similar error if I try to directly access the latitude directly ( placeA.latitude
) 如果我尝试直接直接访问纬度(
placeA.latitude
),则会收到类似的错误
What am I doing wrong? 我究竟做错了什么? I am new to using the Django framework.
我是使用Django框架的新手。
In your model, you are defining the structure. 在模型中,您正在定义结构。 You can't use
你不能用
placeAlatitude = placeA.getLatitude()
placeBlatitude = placeB.getLatitude()
placeAlatitude and placeBlatitude must be define the type of model. placeAlatitude和placeBlatitude必须定义模型的类型。 You can't simply return value.
您不能简单地返回值。
From analyzing your model these two fields are not necessary it is redundant values(same case for placeAlongitude and placeBlongitude). 通过分析模型,这两个字段不是必需的,这是多余的值(placeAlongitude和placeBlongitude相同)。 You defined a foreign key relation to location.
您定义了与位置的外键关系。 So you can access longitude and latitude by using that relation.
因此,您可以使用该关系来访问经度和纬度。
First of all there is likely an error in your Location
model: you here do not construct the FloatField
s and IntegerField
: you only pass a reference to the class, you do not "call" it. 首先,您的
Location
模型中可能有一个错误:您在这里不构造 FloatField
和IntegerField
:您只传递对类的引用,而不是“调用”它。 So in order to define these columns, you should add parenthesis: 因此,为了定义这些列,您应该添加括号:
class Location(models.Model):
name = models.CharField(max_length=200)
latitude = models.FloatField
()
longitude = models.FloatField
()
altitude = models.IntegerField
()
# ...
Next you can not define placeAlatitude
, etc. with placeA.GetLatitude()
, at class level placeA
is a ForeignKey
object, not a Location
, object, you can however define a @property
here that will, for a Distance
object, fetch the correct attribute of the related objects: 接下来,您不能定义
placeAlatitude
等与placeA.GetLatitude()
的一流水平, placeA
是一个ForeignKey
对象,而不是一个Location
,对象,但是,您可以定义一个@property
这里会,对于Distance
对象,获取正确的相关对象的属性:
class Distance(models.Model):
placeA = models.ForeignKey(Location, on_delete=models.CASCADE)
placeB = models.ForeignKey(Location, on_delete=models.CASCADE)
distance = models.FloatField(default = 0.0)
@property
def
placeAlatitude(self):
return self.placeA.latitude
@property
def
placeBlatitude(self):
return self.placeB.latitude
@property
def
placeAlongitude(self):
return self.placeA.longitude
@property
def
placeBlongitude(self):
return self.placeB.longitude
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.