我希望有人能帮助我。我是django的学生和初学者。我试图允许某些高级用户查看特定书籍的高级内容。我已经有书籍,用户和会员类型的模型。但是,我想知道如何连接这3个目录,以便某个用户只能从书籍列表中看到 ONE 本书的允许的高级内容
这是代码示例:
Class Book(models.Model)
code = models.SlugField(unique=True, help_text="Enter BIC Code", null=True)
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.CASCADE, null=True)
artist = models.ForeignKey('Artist', on_delete=models.CASCADE, null=True)
summary = models.TextField(max_length=1000, help_text="Enter a brief description of the book")
book_type = models.ForeignKey('Type', on_delete=models.SET_NULL, null=True)
isbn = models.CharField('ISBN', max_length=15, help_text='Enter ISBN number')
status = models.CharField(choices=STATUS_CHOICES, default='Ongoing', null=True, max_length=30)
genre = models.ManyToManyField(Genre, help_text="Select a genre for this book")
language = models.CharField(max_length=50, null=True,
help_text="Enter the book's natural language (e.g. English, Tagalog, Japanese etc.)")
released_date = models.CharField(max_length=50, null=True, help_text="Enter the Released Date (e.g. March 2014)")
cover = models.ImageField(upload_to='book_cover', null=True, validators=[img_file_extension],
help_text="Image File Only")
teaser = models.FileField(upload_to='book_preview', validators=[pdf_file_extension], help_text="PDF File Only")
featured = models.BooleanField(null=True, default=False)
timestamp = models.DateTimeField(default=timezone.now)
def display_genre(self):
return ', '.join([genre.name for genre in self.genre.all()[:3]])
display_genre.short_description = 'Genre'
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('book-detail', args=[str(self.id)])
def __str__(self):
return self.title
class Page(models.Model):
book = models.ForeignKey('Book', on_delete=models.CASCADE, null=True)
file = models.FileField(upload_to='book_content', validators=[pdf_file_extension], help_text="PDF File Only")
def __str__(self):
return self.book.title
def get_absolute_url(self):
return reverse('page-detail', kwargs={'id', self.id})
类成员(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
title = models.ManyToManyField(Book)
premium = models.BooleanField(default=False)
reference = models.CharField(max_length=40, blank=True)
def __str__(self):
return self.user.username
我不知道在views.py之后该怎么办
class PageDetailView(LoginRequiredMixin, generic.View):
def get(self, request, *args, **kwargs):
book = get_object_or_404(Book)
page = get_object_or_404(Page)
membership = get_object_or_404(Membership, user=request.user)
context={'object': None}
if #-----------------------
return render(request, "catalog/page_detail.html", context)