简体   繁体   中英

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM