是否可以将基于django泛型类的视图与数据库视图一起用作模型?

问题描述 投票:-2回答:1

这是我的问题,我正在建立一个q&a网站,我有问题,标签,类别作为模型。我想在其他地方使用基于django泛型类的视图来显示数据库中的问题列表。

我已经使用Question类完成了这个,但是我没有属于其他模型的所有细节,比如问题类别和标签,所以我决定创建一个数据库视图。

这是我的模型模块

from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField




class QuestionCategory(models.Model):

    name = models.CharField(max_length = 255, help_text="question")
    created_at = models.DateTimeField(auto_now_add=True)
    last_edited_at = models.DateTimeField(auto_now=True)
    image = models.FileField()


    def __str__(self):

        return self.name

class QuestionTag(models.Model):
    title = models.CharField(max_length=255, primary_key=True)
    description = models.TextField(null = True)

    def __str__(self):
        return self.title


class Question(models.Model):

    title = models.CharField(max_length=1024)
    nb_views = models.IntegerField(default=0)
    anonymously = models.BooleanField(default = True)
    created_at = models.DateTimeField(auto_now_add=True)
    last_edited_at = models.DateTimeField(auto_now=True)
    details = RichTextUploadingField(null = True)
    #une question appartient à une seule catégorie
    category = models.ForeignKey('QuestionCategory', on_delete=models.SET_NULL, null=True)
    tags = models.ManyToManyField(QuestionTag, help_text="Selectionnez un tag pour cette question")

    def __str__(self):
        return self.title


    def get_absolute_url(self):
        return reverse('question-detail', args=[str(self.id)])

class Answer(models.Model):
    nb_upvotes = models.IntegerField(default=0)
    nb_downvotes = models.IntegerField(default=0)
    nb_views = models.IntegerField(default = 0)
    details = RichTextUploadingField()
    question = models.ForeignKey(Question, on_delete=models.SET_NULL, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    last_edited_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return "Answer " + str(self.id) 


class AnswerComment(models.Model):
    nb_upvotes = models.IntegerField(default=0)
    nb_downvotes = models.IntegerField(default =0)
    details = models.TextField()
    answer = models.ForeignKey(Answer, on_delete=models.CASCADE)
    parent = models.ForeignKey('self', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    last_edited_at = models.DateTimeField(auto_now=True)


class View360Question(models.Model):
    id = models.BigIntegerField(primary_key=True)
    title = models.TextField(max_length=1024)
    nb_views = models.IntegerField()
    created_at = models.DateTimeField()
    last_edited_at = models.DateTimeField()
    nb_answers = models.IntegerField()
    tags = models.TextField()
    category_name = models.TextField()
    category_id = models.IntegerField()

    def __str__(self):
        return self.title

    class Meta:
        managed = False
        db_table = "qanda_view360question"

这是我的观点模块

from django.shortcuts import render
from django.views import generic
from .models import Question, View360Question, QuestionCategory, Answer, QuestionTag, AnswerComment

# Create your views here.

def index(request):

    #on récupère le nombre de questions
    num_questions = Question.objects.all().count()

    #nombre de questions anonymes
    num_anonym_questions = Question.objects.filter(anonymously= True).count()

    #nombre de réponses
    num_answers = Answer.objects.all().count()

    #nombre de Catégories
    num_categories = QuestionCategory.objects.all().count() 

    return render(
        request,
        'index.html',
        context={'num_questions': num_questions, 'num_anonym_questions': num_anonym_questions,
        'num_answers': num_answers, 'num_categories': num_categories}
    )



def qaIndex(request):

    return render(request, 'qa.html')


def categoriesIndex(request):

    return render(request, 'categories_index.html')


class View360QuestionListView(generic.ListView):
    model = View360Question
    template_name = 'questions/questions_list.html'

在View360QuestionListView中,当我将模型更改为Question它从数据库中获取项目时,但当我回到View360Question这是我创建的视图时,我总是获取一个空数组,但在数据库中视图返回项目。

python django django-models
1个回答
0
投票

正如Guillermo chamorro所说,问题出在我的模板中,而不是使用我使用的question_list中的viewname_list。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.