Answer the question
In order to leave comments, you need to log in
How to get a field with model id from another model in Django?
There are two models:
class Service(models.Model):
""" Инстаграм; Вконтакте; Youtube и т.п. """
name = models.CharField(verbose_name=u'Наименование', default='Разное', max_length=15, blank=False, null=False)
class Meta:
verbose_name = 'Сервис'
verbose_name_plural = 'Сервисы'
def __str__(self):
return self.name
class Category(models.Model):
""" Подписчики; Лайки; Просмотры и т.п. """
service_id = models.ForeignKey(Service, verbose_name=u'Сервис', on_delete=models.CASCADE)
name = models.CharField(verbose_name=u'Наименование', default='Разное', max_length=10)
description = models.TextField(verbose_name=u'Описание', blank=True, null=True)
class Meta:
verbose_name = 'Категория'
verbose_name_plural = 'Категории'
def __str__(self):
return self.name
<div class="col-xl-10 pb-5">
{% for category in categories %}
<div class="category-item" data-service="{{ category.service_id }}">
</div>
{% endfor %}
</div>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question