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.

On Wednesday, 19 June 2019 21:10:37 UTC+5:30, Андрей Сердюк wrote:
>
> 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1d26ea00-5cca-4e66-b66b-808df8397eaf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to