L
L
leha782016-04-20 21:04:08
Django
leha78, 2016-04-20 21:04:08

How to get records from db where category = category?

class Razdely (models.Model):
    title = models.CharField(verbose_name=_('Заголовок'), max_length=256, null=True, blank=True)
    alias = models.CharField(verbose_name=_('Алиас'), max_length=256, null=True, blank=True)

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name = _('Categories')
        verbose_name_plural = _('Разделы')


class Classifieds (models.Model):

    title = models.CharField(verbose_name=_('Заголовок'), max_length=256)
    description = models.TextField(verbose_name=('Содержание'))
    image = models.FileField(verbose_name=_('Изображение'), upload_to='Classifieds', null=True, blank=True)
    phone = models.CharField(verbose_name=_('Телефон'), max_length=256, null=True, blank=True)
    category = models.ForeignKey(Razdely, verbose_name=_('Категория'), null=True, blank=True)
    created_at = models.DateTimeField(verbose_name=('Date create'), auto_now_add=True)
    user_id = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('User'), related_name='users')

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name = _('Classifieds')
        verbose_name_plural = _('Объявления')

I need to display all posts belonging to a certain category. How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maa-Kut, 2016-04-20
@leha78

Something like:

cf = Classfields.objects.filter(category__title = 'Нужный заголовок')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question