Fix Python – Django filter queryset __in for *every* item in list

Let’s say I have the following models
class Photo(models.Model):
tags = models.ManyToManyField(Tag)

class Tag(models.Model):
name = models.CharField(max_length=50)

In a view I have a list with active filters called categories.
I want to filter Photo objects which have all tags present in categories.
I tried:
Photo.objects.filter(tags__n….

Fix Python – How to obtain a QuerySet of all rows, with specific fields for each one of them?

I have a model Employees and I would like to have a QuerySet of all rows, but with some specific fields from each row, and not all fields.
I know how to query all rows from the table/model:
Employees.objects.all()

I would like to know how to select fields for each of the Queryset element. How can I do that?
….

Fix Python – .filter() vs .get() for single object? (Django)

I was having a debate on this with some colleagues. Is there a preferred way to retrieve an object in Django when you’re expecting only one?
The two obvious ways are:
try:
obj = MyModel.objects.get(id=1)
except MyModel.DoesNotExist:
# We have no object! Do something…
pass

And:
objs = MyModel.objects.filter(id=1)

if len(objs) == 1:….

Fix Python – How to do a less than or equal to filter in Django queryset?

I am attempting to filter users by a custom field in each users profile called profile. This field is called level and is an integer between 0-3.
If I filter using equals, I get a list of users with the chosen level as expected:
user_list = User.objects.filter(userprofile__level = 0)

When I try to filter using less than:
user_list = User.objects…..