Vlad
Vlad

Reputation: 977

django, many to many relation get_field name

I had two django models connected with many to many relationship.

First model:

class Category(models.Model):

   name = models.CharField(max_length=255)
   products = models.ManyToManyField(Product, related_name='categories',
                                  blank=True, null=True,
                                  verbose_name=_('Products'),
                                  )

second model:

class Product(models.Model):

    description = models.TextField(verbose_name=_('Description'), default='')
    manifactor = models.CharField(verbose_name=_('Manifactor'), default='Blackberry', max_length=255)

ok, so:

product = Product.objects.all()[0]
product.categories  - give me a list of categories for this product.

but:

product._meta.many_to_many - return empty list [].
and product._meta.get_field('categories') - return None.

Why ?

How can I get the verbose name of category field from product object ?

Upvotes: 0

Views: 1351

Answers (1)

Ponimas
Ponimas

Reputation: 166

You can add

categories = models.ManyToManyField(Category, 
                                    through=Category.products.through)

to your Product model

Upvotes: 2

Related Questions