I've got models like those in django:
class User(models.Model):
name = models.CharField(max_length = 128)
class Message(models.Model):
sender = models.ForeignKey(User, related_name = 'messages_sent')
recipient = models.ForeignKey(User, related_name = 'messages_recieved')
subject = models.CharField(max_length = 128)
body = models.CharField(max_length = 3500)
class Response(models.Model):
message = models.OneToOneField(Message, primary_key = True)
reply = models.TextField()
and I'm trying to get all the messages for the user that have no response, something I would write in SQL as:
select * from user u
join message m on (u.id = m.recipient_id)
left join response r on (m.id = r.message_id)
where r.message_id = null
I'd think the natural way to do this would be:
u.messages_recieved.filter(response = None)
or
u.messages_recieved.filter(response__message_id__isnull = True)
but the SQL generated always ends up being:
WHERE ("project_message"."recipient_id" = 1 AND "project_message"."id" IS NULL)
Am I doing something stupid, or is this a bug in Django?