【问题标题】:django contains model with ManyToMany fields, but they are not available with querydjango 包含具有 ManyToMany 字段的模型,但它们不适用于查询
【发布时间】:2012-10-25 12:45:30
【问题描述】:

我有一个包含两个 ManyToMany 字段的模型,并且我已成功创建包含这些字段的条目。当我创建一个查询集时,这里的数据不包括在内。我如何访问这些数据?

查询集代码:

example_list = Example.objects.values_list().order_by('-date_submitted')

模型.py

class Example(models.Model):
    example_id = models.ForeignKey(User)
    date_submitted = models.DateTimeField()
    title = models.CharField(max_length=70)
    description = models.TextField()
    file = models.FileField(upload_to='files')
    photo = models.FileField(upload_to='design_photos')
    materials = models.ManyToManyField('Materials')
    tags = models.ManyToManyField('Tags')

【问题讨论】:

  • 你的意思是表示关系的数据不存在,还是外来物的相关数据不存在?例如。它是向您显示材料 = 材料 1、材料 2,还是完全忽略这两个字段?

标签: django django-orm


【解决方案1】:

我已经通过使用过滤器的方法解决了这个问题,如下:

example_list = Example.objects.filter(tags__tag__contains='jewellery').order_by('-date_submitted')[:10]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    相关资源
    最近更新 更多