python - Django Category Model and Template View Issue -


my question 2 phased it's same django model. trying category model work petition model. currently,

i 'fielderror' error when define queryset 'categoryview' class:

cannot resolve keyword 'created_on' field. choices are: description, id, petition, slug, title 

here code 'categoryview' class:

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] 

here code in models.py:

class category(models.model):     title = models.charfield(max_length=90, default="select 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 external video link")     petition = models.textfield(null=false, default="type 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() 

i 'fielderror' on category view template (category.html) when 'get_queryset()' has been defined.

when comment out, page displays posts not retrieved; list of categories instead. here category view template(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 %} {% 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}}  {% if petition.created_by == user %}  {% 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 no posts in database</p>  {% endif %} </div>   <div class="col-md-4"> <h3>topics</h3> <ul>     {% petition in category_list %}     <li><a href="#">{{petition.title}}</a></li>     {% endfor %} </ul> </div>  </body> </html> 

what doing wrong? please help.

that's because trying order category's queryset created_on , category model doesn't have field called created_on. field within petition model. if want order categories petition's created_on should "join" between tables, django's orm easy.

for simplicity on code better name relation

class petition(models.model):     ...     categories = models.manytomanyfield(category, related_name='petitions')     ... 

now can refer category's petitions 'petitions'

category.objects.order_by('-petitions__created_on') 

for use petitions within template best way prefetch petitions on queryset avoid hitting database every time in loop.

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

that bring every petition each category. in template can use:

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

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -