Subito
Subito

Reputation: 227

Querying all objects that don't exist in another model

I have two models, Picture and SubmittedPicture as follows:

class Picture(models.Model):
user = models.ForeignKey(User)
pic = ImageField(upload_to='userpics/%Y/%m/%d/%H')

class SubmittedPicture(models.Model):
picture = models.ForeignKey(Picture, unique=True)
description = models.TextField()
submitted_time = models.DateTimeField(auto_now_add=True)

Now, I need to query for all Pictures which do not have a corresponding SubmittedPicture. I tried several options, but none of them was functional.

I read through the Django-doc, but couldn't find something useful.

Thanks in advance!

Upvotes: 1

Views: 71

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239460

Picture.objects.filter(submittedpicture__isnull=True)

Upvotes: 2

Related Questions