class Business(models.Model):
manager = models.ForeignKey(User, on_delete=models.CASCADE)
#...
class Event(models.Model):
business = models.ForeignKey(Business, on_delete=models.CASCADE)
text = models.TextField()
when = models.DateTimeField()
likes = GenericRelation('Like')
class Like(models.Model):
person = models.ForeignKey(User, on_delete=models.CASCADE)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
date = models.DateTimeField(auto_now=True)
So I have this structure in models.py
. Here's an explanation of important models:
Event model has "business" field which links to the certain Business object, which further has "manager" field. Also, Event model has "when" field which describes the date when an event will occur.
On the other side, Like model has generic foreign key field which can link to the certain Event object, and also "person" and "date" fields which describe who gave like and when it was given to that event.
The goal is now to display on user page all liked events by targeted user, and all events which manager is that user. It can be simply done with this SQL command:
SELECT event.*
FROM event
INNER JOIN
business
ON (event.business_id = business.id)
LEFT JOIN
'like'
ON (event.id = object_id AND content_type_id = 17)
WHERE ('like'.person_id = 1 OR business.manager_id = 1);
But now the results have to be sorted, by already mentioned "date" in Like and "when" in Event model. The sorting behavior should be as follows: If the Event object derives from Like object then it should be sorted by "date" in that Like object, in other case it should be sorted by "when" in Event - this is where the things change. Therefore, here's how the final raw query looks like:
SELECT event.*
FROM event
INNER JOIN
business
ON (event.business_id = business.id AND business.manager_id = 1)
LEFT JOIN
'like'
ON (event.id = object_id AND content_type_id = 17 AND person_id = 1)
ORDER BY COALESCE('like'.date, event.'when') DESC;
And I have now to translate that last query to Django ORM, but I'm completely lost on that part. Can anyone help me? Thanks in advance!