Answer the question
In order to leave comments, you need to log in
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
def kruzhki(request):
q = Cruski.objects.all()
p = Vospitately.objects.filter(vospet_crushek_id=?)
return render(request,'krushki.html',{'kruzki':q, 'vospitately':p})
{% 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 %}
Answer the question
In order to leave comments, you need to log in
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
def kruzhki(request):
q = Cruski.objects.all()
return render(request,'krushki.html',{'kruzki':q})
{% 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 %}
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
def kruzhki(request):
q = Cruski.objects.all()
return render(request,'krushki.html',{'kruzki':q})
{% 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 questionAsk a Question
731 491 924 answers to any question