How do I make such a thing. There is an activity class, there is an occupation class (lesson). Each activity has a type and date. The number of types is certainly small. I need to sort objects of type occupation by date of activity of a particular type. I can not figure out how to do it. Is it possible to stir up something like this:
Leson.objects.filter(action__type="w").order_by(action__type="w", "-action__date").distinct()
But of course it will not work. And how to write so that it works?
Update : The idea is that I first filter by activity, and then I want to sort by activity date. The problem is that you need to sort by activity of a particular type. For example, I filtered the objects. I got the following
Object A has fields
активность { тип: "w", дата 11-01-01 } активность { тип: "g", дата 11-04-07 } активность { тип: "r", дата 11-03-06 } ....
Object B has fields
активность { тип: "w", дата 11-10-01 } активность { тип: "r", дата 11-03-03 } ....
That is, only those objects that have activities like "w" remain. And the question is, how now to sort them by the date of activity type "w"? If you simply write order_by("-action__date")
, then it will sort by all the activities of this object, and I need to take into account only a certain type.
If you select objects in an array, and then manually sort, the idea is not the best, there are a lot of objects, and there are many activities for each object.
Update 2 :
class Lesson(BaseModel): __metaclass__ = LessonMetaClass ... # Еще много полей class Action(BaseModel): lesson = models.ForeignKey('Lesson', null=True, related_name="actions") type = models.CharField(max_length=32) date = models.DateTimeField(default=datetime.datetime.now)
On exit, I want to get a sorted QuerySet
.