2016-01-21 69 views
0

我的问题是两个阶段,但它来自相同的Django模型。我试图让我的类别模型与我的请愿模型正常工作。目前,Django分类模型和模板视图问题

我得到这个“FieldError”错误,当我定义的查询集为我的“类别查看”类:

Cannot resolve keyword 'created_on' into field. Choices are: description, id, petition, slug, title 

下面是“类别查看”类的代码:

class CategoryView(generic.ListView): 
    model = Category 
    template_name = 'petition/category.html' 
    context_object_name = 'category_list' 

    def get_queryset(self): 
     return Category.objects.order_by('-created_on')[:10] 

以下是我的models.py中的代码:

class Category(models.Model): 
    title = models.CharField(max_length=90, default="Select an appropriate category") 
    slug = models.SlugField(max_length=200, unique=True) 
    description = models.TextField(null=False, blank=False) 

    class Meta: 
     verbose_name_plural = "Categories" 

    def __str__(self): 
     return self.title 

    def get_absolute_url(self): 
     return "/categories/%s/"%self.slug 

class Petition(models.Model): 
    title = models.CharField(max_length= 90, default="Enter petition title here") 
    created_on = models.DateTimeField(auto_now_add=True) 
    image = models.ImageField(null=False, upload_to=imageupload) 
    video = models.CharField(max_length=600, default="Enter an external video link") 
    petition = models.TextField(null=False, default="Type your petition here") 
    created_by = models.ForeignKey(User) 
    category = models.ManyToManyField(Category) 

    def total_likes(self): 
     return self.like_set.count() 

    def __str__(self): 
     return self.title[:50] 

    def get_signatures(self): 
     return self.signature_set.all() 

我在我的类别视图模板(类别)上得到'FieldError'当'get_queryset()'被定义时,

当我注释它时,页面显示但帖子不被检索;我得到一个类别列表,而不是。这里是我的分类视图模板(category.html):

{% include 'layout/header.html' %} 
{% load humanize %} 

    <div class="container content"> 
    <div class="row"> 
    <div class="col-md-8 post-area"> 
    {% if category_list %} 
{% for petition in category_list %} 
<div class="petition-block"> 
<h2 class="home-title"><a href="{% url 'detail' pk=petition.id %}">{{petition.title}}</a></h2> 
<span class="petition-meta"> 
Created {{petition.created_on|naturaltime}} by 
{% if petition.created_by == user %} 
You 
{% else %} 
@{{ petition.created_by }} 
{% endif %} 
    {% if petition.created_by == user %} 
    <a href="{% url 'editpetition' pk=petition.id %}">Edit</a> 
    {% endif %} 
    {% if petition.created_by == user %} 
    <a href="{% url 'deletepetition' pk=petition.id %}">Delete</a> 
    {% endif %} 
</span> 

    {% if petition.image %} 
     <img src="{{ petition.image.url }}" alt="petition image" class="img-responsive" /> 
    {% endif %} 

</div><!--//petition-block--> 
{% endfor %} 

{% else %} 

    <p>Sorry, there are no posts in the database</p> 

{% endif %} 
</div> 


<div class="col-md-4"> 
<h3>Topics</h3> 
<ul> 
    {% for petition in category_list %} 
    <li><a href="#">{{petition.title}}</a></li> 
    {% endfor %} 
</ul> 
</div> 

</body> 
</html> 

我在做什么错?请帮忙。

回答

0

这是因为您试图通过created_on对类别的查询集进行排序,并且您的类别模型没有任何称为created_on的字段。该字段在请愿模式内。如果你想通过请愿的created_on来排列类别,你应该在表格之间做一个“连接”,但是用Django的ORM很容易。

为了简化您的代码是更好的命名关系

class Petition(models.Model): 
    ... 
    categories = models.ManyToManyField(Category, related_name='petitions') 
    ... 

现在,您可以参考该类别的请愿为“上访”

Category.objects.order_by('-petitions__created_on') 

对于使用请愿模板最好的内方式是预取查询集上的请求,以避免每次在for循环中触发数据库。

Category.objects.prefetch_related('petitions').order_by('-petitions__created_on') 

这将带来每个类别的每个请愿。因此,在您的模板中,您可以使用:

{% for category in category_list %} 
    {% for petition in category.petitions.all %} 
     ... 
     @{{ petition.created_by }} 
     ... 
    {% end for %} 
{% end for %} 
+0

感谢您的回应。该值在我的查询与设置返回你的建议,我仍然得到同样的错误:'无法解析关键字“petition_set__created_on”到field.' –

+0

好吧,我把它改成: '高清get_queryset(个体经营): \t \t返回Category.objects.order_by(' - petition__created_on')[:10]'现在它的加载正常,除了模板(category.html)仍然返回类别列表而不是显示类别中的帖子 –

+0

啊,那是因为你只是将类别传递给模板。看看listview是如何工作的http://ccbv.co.uk/projects/Django/1.9/django.views.generic.list/ListView/。我会编辑答案来完成你的问题 – harveydf