sdnaghdi
sdnaghdi

Reputation: 587

How to add object to queryset?

my sample code is :

from django.db import models
from django.contrib.auth.models import User

class Category(models.Model):
    user = models.ForeignKey(User, null=True)
    ....

I want do some thing like this in view :

user = User.objects.none()

categories = Category.objects.filter(**filter)

for category in categories:

    # my problem is here , how add user category object to queryset
    # or merge object and queryset
    user = user | category.user

this part of code is for showing problem :

    user = user | category.user

thanx .

Upvotes: 2

Views: 6114

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599480

You can't do that. A Queryset is a container for objects that satisfy a query, hence the name. It's not a container for arbitrary objects.

Here the correct thing to do is to query the users you need directly. You can follow the relationship to the Category object by using the double-underscore syntax:

users = User.objects.filter(category__criteria=value)

Upvotes: 2

Related Questions