How to access manytomany field content

28 views
Skip to first unread message

Alagiri Rajesh

unread,
Jun 19, 2019, 8:43:41 AM6/19/19
to Django users
Hi I am using the "Django Category" app (https://github.com/praekelt/django-category)

I have installed the same and also have implemented the same. How do i access the category title and other details. Have tried to use the documentation but using "Category.title" shows up blank.

Also have tried multiple combinations but it does not work.

Андрей Сердюк

unread,
Jun 19, 2019, 11:40:37 AM6/19/19
to Django users
Hi, if Your code looks like this:

class Category(models.Model):
name = models.CharField(maxlength=32)
pass

class Goods(models.Model):
categories = models.ManyToManyField(Category, on_delete=models.CASCADE)

You would get it with this code:
category = Category.objects.get(pk=23)
goods = category.goods_set.all()
- it returns queryset of instances, which depends on category with pk=23.
Or like that:
item = Goods.objects.get(pk=1)
categories = item.categories.

Alagiri Rajesh

unread,
Jun 19, 2019, 12:09:20 PM6/19/19
to Django users
Hi @Андрей Сердюк,

Thank you for quick response.

Some clarifications please?

So you are saying that i need to create a new class for the category? the current code for one of my app where i am using category field is as below:

class Listing(models.Model):
    user            = models.ForeignKey(User, default=1, null=True, on_delete=models.SET_NULL)
    title           = models.CharField(max_length=120)
    description     = models.TextField()
    phone_number    = PhoneNumberField(blank=True, null=True)
    fax_number      = PhoneNumberField(blank=True, null=True)
null=True)
    address1        = models.ForeignKey(Address, on_delete=models.CASCADE, related_name='addresses', blank=False, null=False)
    address2        = AddressField(related_name='Address', on_delete=models.CASCADE, blank=False, null=False)
    created_date    = models.DateTimeField(auto_now=False, auto_now_add=True, null=False, blank=True)
    updated         = models.DateTimeField(auto_now=True)
    timestamp       = models.DateTimeField(auto_now_add=True)
    # category        = models.ForeignKey(category, 'category.Category', help_text='Categorize this item.')
    categories      = models.ManyToManyField('category.Category', help_text='Categorize this item.')
    tags            = models.ManyToManyField('category.Tag', help_text='Tag this item.')
    slug            = AutoSlugField(populate_from='title', unique=True, null=False, blank=False)

I do not have a class for Category. I assumed that the django-category app would have it?

Also if i am to retrieve individual or multiple categories / tags related to the listing, how do i show the field in the form and also on the frontend?

Have currently tried to use "{{ Listing.Category.slug }}" / "{{ Listing.Category.title }}" these does not show up on the front end. It shows up blank. screen shot below.


Please advice.

Андрей Сердюк

unread,
Jun 19, 2019, 1:21:28 PM6/19/19
to Django users
So You should to show Your template first and use ModelForm to inherit it with model You need in meta options.
Reply all
Reply to author
Forward
0 new messages