Answer the question
In order to leave comments, you need to log in
How to pull a Forenkey object?
Hello.
There are about 10 categories, each category has 5-10 sites. The task is to display on the page:
Category + its sites,
Category + its sites,
etc.
Models:
class Field(models.Model):
category = models.CharField(default="0", max_length=200, help_text="Отрасль бизнеса клиента")
def __str__(self):
return self.category
class Site(models.Model):
link_to_site = models.ForeignKey(Field, on_delete=models.CASCADE, related_name="link", blank=True, null=True)
site = models.CharField(max_length=200)
title = models.CharField(max_length=200)
def who(request):
categories = Field.objects.all()
context = {
"categories" : categories,
}
return render(request, 'marketing/who.html', context)
{% for category in categories %}
<p>{{ category.category }}</p>
{% for site in category.site_set %} // {% for site in category.link %}
<p>{{ site.site }}</p>
<p>{{ site.title }}</p>
{% endfor %}
{% endfor %}
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