adrien
adrien

Reputation: 146

Query latest entry filter by criterion

I would like to output the latest entries, filtered by a specific criterion. I have this model :

class Task(models.Model):
    active = models.BooleanField(default=True, verbose_name="Activation")
    kind = models.CharField(max_length=1, default=2)
    mode = models.CharField(max_length=32)
    category = models.ForeignKey(TaskCategory, verbose_name="Catégorie")
    city = models.ForeignKey(AppCity, verbose_name="Ville")
    skill = models.ManyToManyField(SkillKind, null=True, blank=True, verbose_name="Compétence requise")

I want to get the fifth latest entry for each city. How can I achieve this?

Upvotes: 0

Views: 52

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77902

You have no date/datetime field and no explicit ordering so I'm not sure what "latest" means in this context, but anyway: assuming the default ordering is ok for you or you set it in your queryset:

for city in AppCity.objects.all():
    print city.task_set.reverse()[:5]

Upvotes: 1

Related Questions