1

I'm trying to filter a list of lectures which have a foreignkey relation with a model called content. Basically every lecture is related to a specific content. But I'm stuck. below is my code:

models.py

class CourseContent(models.Model):
    title = models.CharField(max_length = 250)
    course = models.ForeignKey(Course, on_delete = models.CASCADE, null = True)

    def __str__(self):
        return self.title

class CourseLecture(models.Model):
    content = models.ForeignKey(CourseContent, on_delete = models.CASCADE, null = True)
    title = models.CharField(max_length = 50)
    file = models.FileField()

    def __str__(self):
        return self.title

views.py

def coursedetails(request, pk):
    course = Course.objects.get(id = pk)
    content = CourseContent.objects.filter(course = course)
    content_ids = content.values_list('id')
    lectures = CourseLecture.objects.filter(content__in = content_ids)
    reviews = course.review_set.all()
    rev_count = reviews.count()
    avg = reviews.aggregate(avg = Avg('rating'))
    total = reviews.aggregate(sum = Sum('rating'))
    print(content_ids)
    return render(request, "main/course_detail_view.html", {"course": course, "lectures": lectures, "content": content , "reviews": reviews, "rev_count": rev_count, "avg": avg, "total": total})

The problem here is that i cannot filter lectures on basis of specific content ids. The objects filtered is showing in all the content rather than showing in their specific section.

Thanks in advance.

3 Answers 3

1

The above implementation should work, but it is not efficient. Here is how I would do:

lectures = CourseLecture.objects.filter(content__course_id = pk)

If you mean to order the lectures by content_id, then just add order_by:

lectures = CourseLecture.objects.filter(content__course_id = pk).order_by('content')

Finally, if you want to show the lectures respect to each content, then probably use the following template code:

{% for c in content %}
    {{ c.title }}
    {% for lecture in c.courselecture_set.all %}
       {{ lecture.title }}
     {% endfor %}
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

0

Try and replace the line with

lectures = CourseLecture.objects.filter(content__pk__in = content_ids)

Refer this Django: What is the correct way to query by foreign key field's id? for more.

Comments

0

You can use related_name in your Foreign Key. So, it will be like:

then you can just use Course.lecture.all(). And it will give you all of the lectures with that Course Foreign Key.

Eg:

class Airport(models.Model):
    city = models.CharField(max_length=64)
    code = models.CharField(max_length=4)

class Flights(models.Model):
    origin = models.ForeignKey(Airport, on_delete=models.PROTECT, related_name="origin")
    destination= models.ForeignKey(Airport, on_delete=models.PROTECT, related_name="destination")
    duration = models.IntegerField()

So, Airport.objects.get(pk=1).origin.all() will give me all Flights that Originate from Airport with Primary Key 1.

Hope this Helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.