我的问题分为两个阶段,但它来自同一个Django模型。我试图让我的分类模型与我的请愿模型一起正常工作。目前,
我得到了这个' FieldError'我为我的' CategoryView'定义查询集时出错。类:
Cannot resolve keyword 'created_on' into field. Choices are: description, id, petition, slug, title
以下是' CategoryView'的代码。类:
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'在我的类别视图模板(category.html)上,当' 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 :(得分:0)
那是因为您尝试按created_on订购类别的查询集,而您的类别模型没有任何名为created_on的字段。那个领域在Petition模型中。如果你想通过请愿书的创建来订购类别,你应该做一个&#34;加入&#34;在桌子之间,但使用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 %}