ForeignRelatedObjectsDescriptor'对象没有属性'all

4 投票
1 回答
5622 浏览
提问于 2025-04-18 04:40

我正在把我的网页应用程序迁移到Django 1.7,遇到了一个很奇怪的错误,也许你们中的某些人知道这是怎么回事。

class Product(models.Model):
    title = models.CharField(max_lenght=100)
    slug = models.SlugField()
    content = models.TextField()

class Gallery(models.Model):
    product = models.ForeignKey(Product, related_name="images")
    original = models.ImageField()

class MyView(DetailView):
    model = Product

   def get_context_data(self, **kwargs):
       ....
       # My error is here, when use this context and parse template
       context["galleries"] = Product.images.all()

给出了以下错误信息:

'ForeignRelatedObjectsDescriptor' object has no attribute 'all'
    response = wrapped_callback(request, *callback_args, **callback_kwargs)

1 个回答

12

试试这个:

context["galleries"] = self.object.images.all()

你需要在你Product模型的一个具体实例上调用它,也就是你的对象。

撰写回答