2

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 .

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I need find the way with best performance ,that`s costly to do some thing like this. but thanx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.