"QuerySet"对象没有属性"videos_set"



我试图获得与课程相关的所有视频,我尝试过curriculum.videos.all,但它返回这个错误,说'QuerySet' object has no attribute 'videos',我也尝试使用curriculum.videos_set.all,然后它显示相同的错误,但这次是'QuerySet' object has no attribute 'videos_set'。这个问题有什么问题呢?

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

现在这是我为它写的视图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)


如果您想获得与课程相关的所有视频,您应该使用filter()这样查询:

<>之前# @login_requiredDef course_details(request, course_slug):如果request.user.is_authenticated:User = request.usercourse =course .objects.get(slug=course_slug)curriculum = curriculum .objects.filter(course=course)serial_number = request.GET.get('lecture')video = Video.objects.filter(curriculum=curriculum).order_by('serial_number')

Note:一般最好使用get_object_or_404而不是get(),它会自动处理http 404错误

使用:

course = get_object_or_404(course,slug=course_slug)

您必须根据课程实例使用Video管理器和过滤器。

试着这样写:

# @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')
...

更多信息见官方文档

相关内容

  • 没有找到相关文章

最新更新