Answer the question
In order to leave comments, you need to log in
How to pull data from django database?
I have create classes which are linked by
models.py
class Lesson(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class StudentsGroup(models.Model):
name = models.CharField(max_length=10)
lessons = models.ManyToManyField(Lesson)
def __str__(self):
return self.name
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
student_group = models.ForeignKey(StudentsGroup, blank=True, null=True, on_delete=models.SET_NULL)
def lessons(request):
get_profile = Profile.objects.get(id = request.user.id)
get_group = StudentsGroup.objects.all()
get_lessons = Lesson.objects.all()
context = {
'get_lesson': get_lessons,
'get_group': get_group,
'get_profile': get_profile,
}
template = 'core/lessons.html'
return render(request, template, context)
<p> Lessons: </p> <br>
{{get_profile}}
{{get_profile.student_group.lessons}}
Answer the question
In order to leave comments, you need to log in
What is incomprehensible?
The profile object (1) is your {{get_profile}} (you didn't define the __str__() method on the model, it's inferred by default).
get_profile.student_group.lessons will return a string representation of the associated Lesson object manager. If you want a list, do a loop:
{% for lesson in get_profile.student_group.lessons.all %}{{ lesson.name }}<br>{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question