如何在多对多Django中列出对象

如何在多对多Django中列出对象,django,many-to-many,django-queryset,Django,Many To Many,Django Queryset,考虑一下这个模型 class Dealership(models.Model): dealership = models.CharField(max_length=50) class Ordered(models.Model): customer = models.ForeignKey("Customer") dealership = models.ManyToManyField("Dealership") status = models.CharField(m

考虑一下这个模型

class Dealership(models.Model):
    dealership = models.CharField(max_length=50)

class Ordered(models.Model):
    customer = models.ForeignKey("Customer")
    dealership = models.ManyToManyField("Dealership")
    status = models.CharField(max_length=2, choices=status_list, default='p')
我试着

并产生错误

Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'dealership'
所有经销商均已订购。

您非常接近:

更改:

[i.dealership for i in q.dealership.all]
致:

下面是一个项目中我的模型的M2M关系的示例输出,它演示了您应该从列表中看到的内容<代码>与共享\u是名为配置文件的模型的M2M字段:

>>> from polls.models import Poll
>>> polls = Poll.objects.all()
>>> for p in polls:
...   print([sw for sw in p.shared_with.all()])
... 
[<Profile: kerri>]
[<Profile: kerri>]
[]
[<Profile: jake>, <Profile: kerri>]
[<Profile: jake>, <Profile: kerri>]
[<Profile: jake>, <Profile: kerri>]
[<Profile: btaylor>]
[<Profile: jake>, <Profile: kerri>]
>>来自polls.models导入Poll
>>>polls=Poll.objects.all()
>>>民意测验中的p:
...   打印([sw for sw in p.shared_with.all()]))
... 
[]
[]
[]
[, ]
[, ]
[, ]
[]
[, ]
它应该是:

q.dealership.all() #gives a list of objects 
您可以直接这样做,而不是使用列表理解(在上述ans中)

示例:(摘自)

创建两个出版物:

p1 = Publication(title='The Python Journal')
p1.save()
p2 = Publication(title='Science News')
p2.save()
p3 = Publication(title='Science Weekly')
p3.save()
现在,创建一篇
文章
,并将
文章
出版物
关联:

a1 = Article(headline='NASA uses Python')
a1.save()
a1.publications.add(p1, p2)
a1.publications.add(p3)

a1.publications.all()
[<Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]
a1=Article(headline='NASA使用Python')
a1.保存()
a1.出版物。添加(p1、p2)
a1.出版物。增补(p3)
a1.出版物。全部()
[, ]

Traceback(最近一次调用):文件“”,第1行,在AttributeError中:“QuerySet”对象没有属性“经销商”回溯(最近一次调用):文件“”,第1行,在AttributeError中:“QuerySet”对象没有属性“经销商”[x代表订单中的x()。\u dict\uu.keys()]['modified_at'、'created_at'、'kiosk_id'、'u state'、'vehicle_id'、'kit_optional_id'、'customer_id'、'status'、'id']经销商不存在。但_state存在。上面的代码应该给您一个
经销商
实例的列表。我不理解您在之前的评论中所指的内容。
q.dealership.all() #gives a list of objects 
from django.db import models

class Publication(models.Model):
    title = models.CharField(max_length=30)

    def __str__(self):              # __unicode__ on Python 2
        return self.title

    class Meta:
        ordering = ('title',)

class Article(models.Model):
    headline = models.CharField(max_length=100)
    publications = models.ManyToManyField(Publication)

    def __str__(self):              # __unicode__ on Python 2
        return self.headline

    class Meta:
        ordering = ('headline',)
p1 = Publication(title='The Python Journal')
p1.save()
p2 = Publication(title='Science News')
p2.save()
p3 = Publication(title='Science Weekly')
p3.save()
a1 = Article(headline='NASA uses Python')
a1.save()
a1.publications.add(p1, p2)
a1.publications.add(p3)

a1.publications.all()
[<Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]