python - queryset django relationship -
i have 2 models:
class income(models.model): id = models.autofield('id', primary_key=true) date = models.datefield('date', blank=true, null=true) user = models.foreignkey(user, null=true, help_text="user income") class invoice(models.model): id = models.autofield('id', primary_key=true) income = models.foreignkey(income, null=true, blank=true, related_name='income') user = models.foreignkey(user, null=true, blank=true, related_name='user_invoice')
and need "income" not associated "invoice". find no way see problem. thank :)
filter using __isnull
:
normally, django automatically follows relationship fields using lowercased name of related model:
income.objects.filter(invoice__isnull=true)
here, you've specified related_name
attribute, need use that:
income.objects.filter(income__isnull=true)
note reveals related name attribute income
fk on invoice
backwards. see no reason setting related_name
@ all, if need 1 should describe how invoices related incomes, not vice versa.
Comments
Post a Comment