#courses/models.py
from django.db import models
from django.urls import reverse
from memberships.models import Membership
class Course(models.Model):
slug = models.SlugField()
title = models.CharField(max_length=120)
description = models.TextField()
allowed_memberships = models.ManyToManyField(Membership)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('courses:detail', kwargs={'slug': self.slug})
@property
def lessons(self):
return self.lessons_set.all().order_by('position')
class Lesson(models.Model):
slug = models.SlugField()
title = models.CharField(max_length=120)
course = models.ForeignKey(Course, on_delete=models.SET_NULL, null=True)
position = models.IntegerField()
video_url = models.CharField(max_length=200)
thumbnail = models.ImageField()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('courses:lesson-detail',
kwargs={
'course_slug': self.course.slug,
'lesson_slug': self.slug
})
#courses/models.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView,View
from memberships.models import UserMembership
from .models import Course, Lesson
# Create your views here.
class CourseListView(ListView):
model = Course
class CourseDetailView(DetailView):
model = Course
class LessonDetailView(View):
def get(self,request,course_slug,lesson_slug, *args,**kwargs):
course_qs = Course.objects.filter(slug=course_slug)
if course_qs.exists():
course = course_qs.first()
lesson_qs = Course.lessons.filter(slug=lesson_slug)
if lesson_qs.exists():
lesson = lesson_qs.first()
user_membership = UserMembership.objects.filter(user=request.user).first()
user_membership_type = user_membership.membership.membership_type
course_allowed_mem_types = course.allowed_memberships.all()
context = {
'object' : None
}
if course_allowed_mem_types.filter(membership_type=user_membership_type).exists():
context = {'object': lesson}
return render(request, "courses/lesson_detail.html",context)
Traceback (most recent call last):
File "C:\Users\ASUS\Envs\web\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\ASUS\Envs\web\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\ASUS\Envs\web\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\ASUS\Envs\web\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\ASUS\Envs\web\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\ASUS\myproject\cpx\courses\views.py", line 20, in get
lesson_qs = Course.lessons.filter(slug=lesson_slug)
AttributeError: 'property' object has no attribute 'filter'
Кто-нибудь может объяснить мою ошибку?