Re: Treat two fields as one in a queryset
Thanks for the suggestions. The "convert to list" approach sounded appealing, but I had to get back a queryset since this was for an RSS feed. After reading up more, finally decided it might actually make more sense to go with a bit of denormalization and create a "combined_date" field on the model, with a custom save() method that keeps the right thing in that new field. def save(self): if self.completed == True: self.combined_date = self.completed_date else: self.combined_date = self.created_date super(Item, self).save() That turned out to be trivial, gave me a single date field to sort on, and kept everything in a queryset. Thanks, ./s -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Treat two fields as one in a queryset
If you know how to do it in SQL, just make your own manager. http://docs.djangoproject.com/en/1.1/topics/db/managers/ Yay for Django! Shawn -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Treat two fields as one in a queryset
I think you'll have to sort the list yourself by creating a calculated field, since you're only returning a small number, I don't see this as a big issue. So, get the records back that you want via the ORM, and then copy them over to a list. HTH John On Feb 20, 6:26 pm, shacker wrote: > Given a model like: > > class Item(models.Model): > title = models.CharField(max_length=140) > created_date = > models.DateTimeField(default=datetime.datetime.now) > completed = models.BooleanField(default=False) > completed_date = models.DateTimeField(blank=True,null=True) > ... > > I want to create a queryset that retrieves the last 30 Items that were > either created OR completed most recently. In other words, the > created_date and the completed_date fields need to be treated as if > they were a single date field on the model, so that the returned items > would be chronological by either date (completed_date would only be > considered for items where completed=True) > > I've tried both: > > .order_by('-completed_date','-created_date')[:30] > > and > > .order_by('-completed_date').order_by('-created_date')[:30] > > but neither of these are quite right. Not sure what the right approach > to this should be. Suggestions? Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.