【问题标题】:How do I know which groups each product is in?我如何知道每个产品属于哪些组?
【发布时间】:2017-11-06 03:36:32
【问题描述】:
class Product(models.Model):
    name = models.CharField(max_length=256)

class Group(models.Model):
    name = models.CharField(max_length=32, unique=False)   
    products = models.ManyToManyField(Product, blank=True)

group.pk == 1 中的产品:

products = Group.objects.get(pk=1).products.all().values('name')

结果:

<QuerySet [
    {"name": "product1"},
    {"name": "product2"},
    {"name": "product3"},
]

我如何知道每个产品属于哪些组?像这样:

<QuerySet [
    {"name": "product1", "groups": [{"pk": 1, "name": "group1"}]},
    {"name": "product2", "groups": []"},
    {"name": "product3", "groups": [{"pk": 1, "name": "group1"}, {"pk": 2, "name": "group2"}},
]

谢谢!

UPD1.

如果我们有数据:

group1 | product 1
group1 | product 2
group2 | product 2
group2 | product 3

创建表(html)的思路如下:

/group/1:

product 1 | dropdown (group1)
product 2 | dropdown (group1, group2)

/组/2:

product 2 | dropdown (group1, group2)
product 3 | dropdown (group2)

【问题讨论】:

  • Group.objects.get(pk=1).products.all().values() 这个返回什么,能发一下吗?
  • Group.objects.get(pk=1).products.all().values() :
  • 所以你在 orm 中得到了组,只需将组放入值中,你就会得到它
  • 您可以尝试map(lambda x: {'name': x.name, 'groups': x.group_set.values('pk', 'name')}, Product.objects.all()),但这会将您返回为列表而不是查询集

标签: django many-to-many django-queryset annotate


【解决方案1】:

据我了解,您希望从多对多字段反向查找模型。

您可以像这样获取产品组:

这里product 是一个产品对象,您想列出与该产品关联的所有组。

product.group_set.all()

【讨论】:

  • 我不仅想看货到群里,还想知道这个产品在其他什么群里。 group1 (product1, product2), group2(product2, prost3) => for group1(product1[0], product2[1]), and for group2 (product1 [0], product2 [1])
  • group2 (product2 [1], product3 [0]) *
【解决方案2】:

values 接受另一个名为 'group' 的参数,使用它来获取相关组。没有给出你想要的预期结果。但给出了主意。

>>> Product.objects.all().values('name', 'group')
<QuerySet [
    {'group': 1, 'name': u'Product 3'}, 
    {'group': 2, 'name': u'Product 3'}, 
    {'group': 1, 'name': u'Product 2'}, 
    {'group': 2, 'name': u'Product 1'}]>

【讨论】:

    【解决方案3】:

    一旦你有一个Product 实例:

    product = Product.objects.get(pk=1)
    

    它知道哪些Group 实例引用了它。使用group_set 字段:

    groups = product.group_set.all()
    

    通过声明一个相关对象(使用ForeignKeyManyToManyField),关系gets an implied field 的“另一端”来访问引用此对象的实例。

    在本例中,该字段名为 group_set,它是引用此 Product 实例的 Group 实例的模型管理器。

    【讨论】:

      【解决方案4】:

      模型.py:

      context['products'] = Group.objects.get(pk=1).products.all()
      return render(request, 'template.html', context)
      

      模板.html:

      {% for product in products %}
          {{ product.name }}
      
          COUNT: {{ product.group_set.count }}
          <ul>
              {% for group in product.group_set.all %}
                  <li>{{ group.name }}</li>
              {% endfor %}
          </ul>
      {% endfor %}
      

      解决方案并不难,因为:

      {% for group in slave_product.group_set.all() %} # ()
      

      谢谢大家!

      【讨论】:

      • 您在问题中从未提及template
      • 是的,我一直在寻找答案在查询中的解决方案,但事实证明这可以使用模板完成
      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 2021-09-22
      • 2020-05-29
      • 2017-02-14
      • 1970-01-01
      • 2020-07-11
      • 2015-07-21
      • 1970-01-01
      相关资源
      最近更新 更多