S
S
Sergey Nizhny Novgorod2016-11-11 05:19:43
Django
Sergey Nizhny Novgorod, 2016-11-11 05:19:43

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)

View.py
def who(request):

    categories = Field.objects.all()

    context = {
        "categories" : categories,
    }

    return render(request, 'marketing/who.html', context)

Template:
I try to pull data through link or site_set - but it doesn't work.
{% 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 %}

Can you tell me how to do it correctly, otherwise I'm stupid and can't figure it out.
For example, using the Site model, it's easy to pull out a category {{ site.link_to_site.category}} , but somehow it doesn't work back.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-11-11
@Terras

{{ categories.link.all }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question