I am trying to get all the videos related to a curriculum, I've tried curriculum.videos.all
but it return this error that says 'QuerySet' object has no attribute 'videos'
, i also tried using curriculum.videos_set.all
then it shows the same error but this time with a 'QuerySet' object has no attribute 'videos_set'
. what can be the issue with this problem?
models.py
class Course(models.Model):
course_title = models.CharField(max_length=100, null=True, blank=True)
slug = models.SlugField(unique=True)
course_thumbnail = models.ImageField(upload_to=user_directory_path, blank=True)
class Curriculum(models.Model):
course = models.ForeignKey(Course , null = False , on_delete=models.CASCADE, related_name="curriculum")
title = models.CharField(max_length = 100 , null = False)
slug = models.SlugField(unique=True)
def __str__(self):
return f'{self.course.course_title} - {self.title}'
class Video(models.Model):
title = models.CharField(max_length = 100 , null = False)
curriculum = models.ForeignKey(Curriculum , null = False , on_delete=models.CASCADE, related_name="videos")
serial_number = models.IntegerField(null=False)
video_id = models.CharField(max_length = 100 , null = False)
def __str__(self):
return self.title
Now this is the view that i have written for it View.py
def course_details(request, course_slug):
if request.user.is_authenticated:
user = request.user
course = Course.objects.get(slug=course_slug)
course.views = course.views 1
curriculum = Curriculum.objects.filter(course=course)
serial_number = request.GET.get('lecture')
# videos = curriculum.videos.all().order_by("serial_number")
# video_count = curriculum.videos.all().order_by("serial_number").count()
videos = Video.objects.filter(curriculum=curriculum).order_by("serial_number")
profile = Profile.objects.get(user=course.course_creator)
user_rating = CreatorRating.objects.filter(profile=profile).aggregate(Avg('rating'))['rating__avg']
profile = Profile.objects.get(user=user)
follow_status = Follow.objects.filter(following=course.course_creator, follower=request.user).exists()
followers_count = Follow.objects.filter(following=course.course_creator).count()
liked = CourseLikes.objects.filter(user=user, course=course).count()
post_tags_ids = course.tags.values_list('id', flat=True)
similar_posts = Course.objects.filter(tags__in=post_tags_ids).exclude(id=course.id)
similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-date_created')
rating_average = CourseRating.objects.filter(course=course).aggregate(Avg('rating'))['rating__avg']
reviews = CourseRating.objects.filter(active=True, course=course).order_by("-date")
reviews_count = CourseRating.objects.filter(active=True, course=course).count()
rating_count = CourseRating.objects.filter(course=course, rating__in=["3.0", "4.0", "5.0"]).count()
discussions = CourseDiscussion.objects.filter(active=True, course=course).order_by('-date')
enrolled_student_count = UserCourse.objects.filter(course=course).count()
user_courses = Course.objects.filter(course_creator=course.course_creator).count()
students_enrolled_for_a_creator = UserCourse.objects.filter(course__course_creator=course.course_creator).count()
all_star = CourseRating.objects.filter(course__course_creator=course.course_creator).count()
# lectures = Video.objects.filter(curriculum=curriculum).count()
recent_courses = Course.objects.filter(course_publish_status="published").order_by("?")[:3]
is_liked = False
if course.likes.filter(id=request.user.id).exists():
is_liked = True
is_saved = False
if profile.favourite_course.filter(slug=course_slug).exists():
is_saved = True
# Course Rating Feature
if request.method == "POST":
form = CourseRatingForm(request.POST)
if form.is_valid():
rating_form = form.save(commit=False)
rating_form.user = request.user
rating_form.course = course
rating_form.save()
messages.success(request, f'You review was sent successfully!')
return redirect("course:course-detail", course.slug)
else:
form = CourseRatingForm
# Course Discusion Feature
if request.method == "POST":
discform = CourseDiscussionForm(request.POST)
if discform.is_valid():
disc_form = discform.save(commit=False)
disc_form.user = request.user
disc_form.course = course
disc_form.save()
messages.success(request, f'Posted!')
return redirect("course:course-detail", course.slug)
else:
discform = CourseDiscussionForm
#For the color of the favorite button
if profile.favourite_course.filter(slug=course_slug).exists():
favorited = True
else:
favorited = False
if liked == True:
liked = True
else:
liked = False
if serial_number is None:
serial_number = 1
# if Video.objects.all().exists
# video, created = Video.objects.get_or_create(serial_number=serial_number, curriculum=curriculum)
video, created = Video.objects.get_or_create(serial_number=serial_number, curriculum=curriculum)
# video_count = Video.objects.get(serial_number=serial_number, course=course)
video.save()
if(video.is_preview is False):
if request.user.is_authenticated is False:
return redirect("course:sign-in")
else:
user = request.user
try:
user_course = UserCourse.objects.get(user=user, course=course)
except:
return redirect("course:check-out", slug=course.slug)
# if request.user.is_authenticated:
# student = request.user.student
# order, created = Order.objects.get_or_create(student=student, completed=False)
# items = order.orderitem_set.all()
# cartItems = order.get_cart_items
# else:
# items = []
course.save()
context = {
'enrolled_student_count':enrolled_student_count,
'user_courses':user_courses,
"reviews" : reviews,
# "lectures" : lectures,
"reviews_count" : reviews_count,
"rating_count" : rating_count,
"followers_count" : followers_count,
"follow_status" : follow_status,
"favorited" : favorited,
"all_star" : all_star,
"liked" : liked,
'recent_courses': recent_courses,
'is_liked': is_liked,
"video" : video,
"form" : form,
'discussions': discussions,
'discform': discform,
# "video_count" : video_count,
'videos':videos,
'course': course,
'students_enrolled_for_a_creator': students_enrolled_for_a_creator,
'is_saved':is_saved,
'rating_average':rating_average,
'user_rating':user_rating,
'similar_posts':similar_posts,
# 'cartItems': cartItems,
}
return render(request, 'course/course-detail.html', context)
else:
user = request.user
course = Course.objects.get(slug=course_slug)
course.views = course.views 1
serial_number = request.GET.get('lecture')
videos = course.videos.all().order_by("serial_number")
video_count = course.videos.all().order_by("serial_number").count()
post_tags_ids = course.tags.values_list('id', flat=True)
similar_posts = Course.objects.filter(tags__in=post_tags_ids).exclude(id=course.id)
similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-date_created')
rating_average = CourseRating.objects.filter(course=course).aggregate(Avg('rating'))['rating__avg']
reviews = CourseRating.objects.filter(active=True, course=course).order_by("-date")
reviews_count = CourseRating.objects.filter(active=True, course=course).count()
rating_count = CourseRating.objects.filter(course=course, rating__in=["3.0", "4.0", "5.0"]).count()
discussions = CourseDiscussion.objects.filter(active=True, course=course).order_by('-date')
enrolled_student_count = UserCourse.objects.filter(course=course).count()
user_courses = Course.objects.filter(course_creator=course.course_creator).count()
students_enrolled_for_a_creator = UserCourse.objects.filter(course__course_creator=course.course_creator).count()
all_star = CourseRating.objects.filter(course__course_creator=course.course_creator).count()
lectures = Video.objects.filter(course=course).count()
recent_courses = Course.objects.filter(course_publish_status="published").order_by("?")[:3]
is_liked = False
if course.likes.filter(id=request.user.id).exists():
is_liked = True
is_saved = False
if serial_number is None:
serial_number = 1
# if Video.objects.all().exists
video, created = Video.objects.get_or_create(serial_number=serial_number, course=course)
video_count = Video.objects.get(serial_number=serial_number, course=course)
video.save()
if(video.is_preview is False):
if request.user.is_authenticated is False:
return redirect("course:sign-in")
else:
user = request.user
try:
user_course = UserCourse.objects.get(user=user, course=course)
except:
return redirect("course:check-out", slug=course.slug)
course.save()
context = {
'enrolled_student_count':enrolled_student_count,
'user_courses':user_courses,
"reviews" : reviews,
"lectures" : lectures,
"reviews_count" : reviews_count,
"rating_count" : rating_count,
'similar_posts':similar_posts,
"all_star" : all_star,
'recent_courses': recent_courses,
"video" : video,
'discussions': discussions,
"video_count" : video_count,
'videos':videos,
'course': course,
'students_enrolled_for_a_creator': students_enrolled_for_a_creator,
'is_saved':is_saved,
'rating_average':rating_average,
}
return render(request, 'course/course-detail.html', context)
CodePudding user response:
You should query like this using filter()
if you want to get all videos related to curriculum:
# @login_required def course_details(request, course_slug): if request.user.is_authenticated: user = request.user course = Course.objects.get(slug=course_slug) curriculum = Curriculum.objects.filter(course=course) serial_number = request.GET.get('lecture') videos = Video.objects.filter(curriculum=curriculum).order_by('serial_number') ...
Note:
It is generally better to useget_object_or_404
instead ofget()
, it automatically handles http 404 error.
Use as:
course = get_object_or_404(Course,slug=course_slug)
CodePudding user response:
You have to use the Video
manager and filter by the curriculum instance.
Try it like this:
# @login_required
def course_details(request, course_slug):
if request.user.is_authenticated:
user = request.user
course = Course.objects.get(slug=course_slug)
curriculum = Curriculum.objects.filter(course=course)
serial_number = request.GET.get('lecture')
videos = Video.objects.filter(curriculum=curriculum).order_by('serial_number')
...
More info in the official docu