B
B
Bulbashful2016-05-25 00:05:43
Django
Bulbashful, 2016-05-25 00:05:43

How to deduce relationships with Foreign Key from the model?

There is a model

lass Cruski(models.Model):
    name = models.CharField(max_length=40)
    description = models.TextField(default='Описание кружка')
    price = models.IntegerField(default=0)
    def __str__(self):
        return self.name

class Vospitately(models.Model):
    fio = models.CharField(max_length=40)
    dolshnost = models.CharField(max_length=80, default='Должность..')
    image = models.ImageField()
    vospet_crushek = models.ForeignKey(Cruski)
    def __str__(self):
        return self.fio

views.py
def kruzhki(request):
    q = Cruski.objects.all()
    p = Vospitately.objects.filter(vospet_crushek_id=?)
    return render(request,'krushki.html',{'kruzki':q, 'vospitately':p})

sample
{% block kruzhki %}
{% for krug in kruzki %}
<p><b>{{ krug.name }}</b></p>
{% for vospet in vospitately %}
<p> {{ vospet.fio }}</p>
{% endfor %}
<p>{{ krug.description }}</p>
<p>{{ krug.price }}</p>
{% endfor %}

How can I link in the model view so that infa is displayed from one and the other model by id?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oscar Django, 2016-05-25
@Bulbashful

Snachala nado opredelit related_name dlya Vospitately.vospet_crushek

class Vospitately(models.Model):
    fio = models.CharField(max_length=40)
    dolshnost = models.CharField(max_length=80, default='Должность..')
    image = models.ImageField()
    vospet_crushek = models.ForeignKey(Cruski', related_name='vospitately')
    def __str__(self):
        return self.fio

Potom vo view mogno tak sdelat
def kruzhki(request):
    q = Cruski.objects.all()
    return render(request,'krushki.html',{'kruzki':q})

I nakonec v shablone pishem what to po tipu takogo
{% block kruzhki %}
{% for krug in kruzki %}
<p><b>{{ krug.name }}</b></p>
{% for vospet in krug.vospitately.all %}
<p> {{ vospet.fio }}</p>
{% endfor %}
<p>{{ krug.description }}</p>
<p>{{ krug.price }}</p>
{% endfor %}

Vot i vse. Takim obrazom u nas vivoditsya krugki i vospitately kotorie v nis sostoyat.
Kstati vam bi opredelitsya stoit kak pisat "cups":
- Cruski
- vospet_crushek
- kruzhki
- kruzki
- krushki.html

S
scream4ik, 2016-05-25
@scream4ik

models.py

class Cruski(models.Model):
    name = models.CharField(max_length=40)
    description = models.TextField(default='Описание кружка')
    price = models.IntegerField(default=0)
    def __str__(self):
        return self.name

class Vospitately(models.Model):
    fio = models.CharField(max_length=40)
    dolshnost = models.CharField(max_length=80, default='Должность..')
    image = models.ImageField()
    vospet_crushek = models.ForeignKey(Cruski, related_name='vospitately')
    def __str__(self):
        return self.fio

views.py
def kruzhki(request):
    q = Cruski.objects.all()
    return render(request,'krushki.html',{'kruzki':q})

template
{% block kruzhki %}
{% for krug in kruzki %}
    <p><b>{{ krug.name }}</b></p>
    {% for vospet in krug.vospitately.all %}
        <p> {{ vospet.fio }}</p>
    {% endfor %}
    <p>{{ krug.description }}</p>
    <p>{{ krug.price }}</p>
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question