If I have
Model.objects.all()
I want to get only one object for any content_object=foo, object_id=N. How can I do that? Say I am ordering by -datetime. If I can get only one object in the queryset for any content_type=foo, object_id=N ... it should be the latest.. How to specify that I only want 1 object for any combination of content_object and object_id?
class CheckIn(models.Model):
...
owner = models.ForeignKey('auth.User')
datetime = models.DateTimeField(editable=False, auto_now=True)
...
# This is the object that should be geocoded, hopefully
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
class Meta:
ordering=( '-datetime', )
Then to take a queryset:
checkins = CheckIn.objects.filter(
datetime__gte=datetime.datetime.now() -
datetime.timedelta(hours=24),
)
this can give me all of the checkins within the last 24 hours but I only want 1 object PER content_type=foo and object_id=N.