简体   繁体   English

“ ForeignKey”对象没有属性

[英]'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模型中可能有一个错误:您在这里不构造 FloatFieldIntegerField :您只传递对类的引用,而不是“调用”它。 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.

相关问题 django 1.8 版“ForeignKey”对象没有属性 - django version 1.8 'ForeignKey' object has no attribute Django / mySQL-AttributeError:“ ForeignKey”对象没有属性“ IntegerField” - Django / mySQL - AttributeError: 'ForeignKey' object has no attribute 'IntegerField' AttributeError: 'ForeignKey' object 没有属性 'types_pizza_set' - AttributeError: 'ForeignKey' object has no attribute 'types_pizza_set' null = True的ForeignKey字段给出'NoneType'对象没有属性ID - ForeignKey field with null=True gives 'NoneType' object has no attribute id Django获取具有外键的对象 - Django Get object if it has foreignkey “用户”对象没有关于外键关系的属性“ get_affected_users” // - 'User' object has no attribute 'get_affected_users'// about foreignkey relation django - 模型unicode()显示foreignkey对象属性 - django - model unicode() show foreignkey object attribute Django:从具有ForeignKey的CSV创建对象 - Django: create object from CSV that has a ForeignKey Django 模板- 如果从外键中获取的 DateTimeField 值传递给 url,则会发生错误:'str' object 没有属性 'strftime' - Django template- If DateTimeField value taken from Foreignkey passed to a url, an error occurs: 'str' object has no attribute 'strftime' ormar 中的 ForeignKey 错误没有属性“get_column_alias” - ForeignKey error in ormar has no attribute 'get_column_alias'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM