Saturday, July 25, 2015

Re: order_by did't work after filtering by foreign key

Do you have multiple posts from the same day that are showing up out of order, but posts across multiple days are at least grouped together by day?

You are using a DateField rather than a DateTimeField, which may lead to unanticipated results when you have multiple entries on the same day. You need the time in there too really determine what is the "newest" entry.

Also, are you performing any sorting/data manipulation within the template? It should just be a standard {% for %} loop there.

-James

On Jul 25, 2015 10:17 AM, "Nguyễn Minh Thế" <nmthe.net@gmail.com> wrote:

I have a model like this

class Category(models.Model):      title = models.CharField(max_length=100, db_index=True)      slug = models.SlugField(max_length=100, db_index=True)        def __str__(self):          return "%s"%self.title    class Video(models.Model):      title = models.CharField(max_length=100, unique=True)      slug = models.SlugField(max_length=100, unique=True)      idlink = models.CharField(max_length=50)      posted = models.DateField(db_index=True, auto_now_add=True)      category = models.ForeignKey(Category)        def __str__(self):          return "%s"%self.title

And my views.py is

def list_video_cat(request, slug):      video = Video.objects.filter(category__slug=slug).order_by('-posted')      return  render(request, 'category.html', {'videos':video})

I want to show my post new to old, but it seems to did not work at this situation. What should I do?


P/S I read this topic before, but it does not solve my problem http://stackoverflow.com/questions/6114718/order-by-doesnt-work-with-filter-in-django-view

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e89357af-054c-43e9-9d8b-a45fbc8f5ae1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVAvtmbTSSo00VPDTNQ6DdhM6B8wBOqokJsv3e8MZAA8w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment