Django ORM: All items not in table -
i've got 2 models:
class schedulerevent(models.model): user = models.foreignkey(user) title = models.charfield(max_length=100) description = models.textfield(blank=true, null=true) class schedulereventreceipt(models.model): event = models.foreignkey(schedulerevent, unique=true) generated_on = models.datetimefield() paid_on = models.datetimefield()
i'm trying figure out how list of schedulerevents aren't in schedulereventreceipt table particular user. kinda thought you'd able this:
inner_qs = schedulerevent.objects.filter(user_id='1') schedulereventreceipt.objects.filter(event__user_id='1').exclude(event__in=inner_qs)
but doesn't work. i'm bit ashamed admit don't know sql enough figure out how .raw()
.
the goal find schedulerevents particular user don't have entry in schedulereventreceipt table.
you filtering events user_id=1
, exclude has events user_id = 1
results in empty queryset.
try this:
qs = schedulerevent.objects.filter(user_id=<user_id>).exclude(schedulereventreceipt__event__user_id=<user_id>)
which excludes corresponding schedulereventreceipt
has current user user_id
Comments
Post a Comment