Anna K
Anna K

Reputation: 1774

How to show a value from model related to other model?

I would like to show all lecture rooms ('sale') which are related with language English('jezyk'='EN')

I can not use such solution:

'sala': Sala.objects.filter(jezyk='3')

Because I defined in the options that user can define languages. In my project English has always "EN" key enter image description here

I try to doing it in this way but it doesn't work

models.py

from django.db import models
from jezyk.models import Jezyk

class Article(models.Model):
    title = models.CharField(max_length=150, verbose_name="Tytul wiadomosci")
    content = models.TextField(verbose_name="Zawartosc")
    published = models.DateTimeField(verbose_name="Data Publikacji")
    jezyk = models.ForeignKey('jezyk.Jezyk', null=True, related_name='jezyk_articles')

views.py

def articles(request):
    return render_to_response('articles.html',{'articles' : Article.objects.all(),'godzina': Godzina.objects.all(),'sala': Sala.objects.all() })

articles.html

{% if article.jezyk = sala.jezyk %}
    {% for sala in sala %}
        <div>{{ sala }}</div>
    {%endfor %}
{% endif %}

Upvotes: 0

Views: 36

Answers (1)

GwynBleidD
GwynBleidD

Reputation: 20539

Just use double underscore notation to get proper field of related model:

'sala': Sala.objects.filter(jezyk__jezyk='EN')

And mixing 2 languages in code (polish and english) is not looking great, try to avoid that.

Upvotes: 1

Related Questions