0
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)

Products in group.pk == 1:

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

Result:

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

How do I know which groups each product is in? Like this:

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

Thanks!

UPD1.

If we have data:

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

The idea of creating a table (html) is as follows:

/group/1:

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

/group/2:

product 2 | dropdown (group1, group2)
product 3 | dropdown (group2)
ns5d
  • 39
  • 1
  • 7

4 Answers4

0

As my understanding, you want to have reverse lookup from your manytomany field to the Model.

You can get the Product groups like this:

Here product is a Product object, and you want to list all the groups associated with that product.

product.group_set.all()
Sijan Bhandari
  • 2,941
  • 3
  • 23
  • 36
  • I want to see not only the goods to the group, but also to find out in what other groups this product. group1 (product1, product2), group2(product2, proudct3) => for group1(product1[0], product2[1]), and for group2 (product1 [0], product2 [1]) – ns5d Jun 05 '17 at 12:03
  • group2 (product2 [1], product3 [0]) * – ns5d Jun 05 '17 at 12:12
0

values takes another argument called 'group' use that to get the related group. Is not giving the expect result you want. But gives idea.

>>> 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'}]>
Raja Simon
  • 10,126
  • 5
  • 43
  • 74
0

Once you have a Product instance:

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

it knows which Group instances refer to it. Use the group_set field:

groups = product.group_set.all()

By declaring a related object (using ForeignKey or ManyToManyField), the “other end” of the relationship gets an implied field to access the instances referring to this one.

In this case, the field is named group_set and it is a model manager for Group instances that refer to this Product instance.

bignose
  • 30,281
  • 14
  • 77
  • 110
0

model.py:

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

template.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 %}

The solution was not difficult, it got confused because of:

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

Thanks to all!

ns5d
  • 39
  • 1
  • 7