简体   繁体   English

访问 ForeignKey 模型属性

[英]Accessing to the ForeignKey model attribute

I'm trying to access to the attribute of the model through a ForeignKey, like this :我正在尝试通过外键访问模型的属性,如下所示:

class Foo(models.Model):
    name = models.CharField('Name', max_length=64)

class FooImage(models.Model):
    model = models.ForeignKey('Foo', related_name='image')
    image = models.ImageField(upload_to='goo/{}/'.format(HERE))

I want the name attribute of Foo where the HERE is.我想要HEREFoo的 name 属性。 I tried many things as :我尝试了很多事情:

self.model.name
model.name
self.name

But it doesn't work.. So, how can I do that ?但它不起作用..那么,我该怎么做?

Refer to django documenation at https://docs.djangoproject.com/en/1.8/ref/models/relations/ .请参阅https://docs.djangoproject.com/en/1.8/ref/models/relations/上的 django 文档。 Here you'll come to know in depth about how related fields can be accessed.在这里,您将深入了解如何访问相关字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM