简体   繁体   中英

Django query over multiple tables

I got so much trouble forming the right query to get my result...

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

class GeoCatch(models.Model):
    user_me = models.ForeignKey(User, null=True, related_name='me')
    user_he = models.ForeignKey(User, null=True, related_name='he')
    permission_he = models.BooleanField(default=False)
    permission_me = models.BooleanField(default=False)

The query I need:

get names in djangos User table of various user_he which have user_me = 1 and have permission_he = True and permission_me= True

This and a couple other querys were not successful...

get_friends =  User.objects.values_list('username').filter(geoCatch__user_me=id)/
.filter(permission_he = True).filter(permission_me= True)

Gives me Error:

django.core.exceptions.FieldError: Cannot resolve keyword 'geoCatch' into field.

friends = GeoCatch.objects.filter(user_me=User.objects.get(pk=id)) \
                    .filter(permission_me=True) \
                    .filter(permission_he=True).user_he_set.all()
# id is an id of a user object
get_friends =  User.objects.values_list('username', flat=True)\
    .filter(me__user_me__id=id, me__permission_he = True, me__permission_me= True)

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