django model filter and condition code example
Example 1: and condition with or in django
Just adding this for multiple filters attaching to Q object, if someone might be looking to it. If a Q object is provided, it must precede the definition of any keyword arguments. Otherwise its an invalid query. You should be careful when doing it.
an example would be
from django.db.models import Q
User.objects.filter(Q(income__gte=5000) | Q(income__isnull=True),category='income')
Here the OR condition and a filter with category of income is taken into account
Example 2: filter with different operator in django
from myapp.models import Entry
from django.db.models import Q
Entry.objects.filter(~Q(id=3))
Example 3: filter django or
from django.db.models import Q
Item.objects.filter(Q(creator=owner) | Q(moderated=False))
Example 4: and in django query filter
Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)
Example 5: django delete object
SomeModel.objects.filter(id=id).delete()
Example 6: objects.filter django
>>> Entry.objects.filter(blog_id=4)