过滤对象,如果其所有外键都在字典中 [英] Filter objects if all its foreignkeys are in a dictionary

查看:125
本文介绍了过滤对象,如果其所有外键都在字典中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是允许用户通过过滤相关对象来访问 Parent 对象的列表Kid



<我们有一个有100个孩子的字典。和平均有5个孩子的父母。

如果1个父对象中的孩子都在字典中,我希望他们被列出。
但不是所有的100个孩子都必须与父对象相关。



例如,如果父母有4个孩子在字典中,1个不是。然后不要包含它们。



我有下面的代码



models.py

  class Parent(models.Model):
title = models.CharField(max_length = 250)
address = models.CharField(max_length = 250)


class Kid(models.Model):
family = models.ForeignKey(Parent)
name = models.CharField(max_length = 250)
age = models.IntegerField()
city = models.CharField(max_length = 250)

Views.py

  def index请求):
patterns = [
{'name':'samy','age__lt':15,'city':'paris'},
{'name':'sally' ,'age__gt':20,'city':'london'}
]
filter_q = reduce(operator.or_,map(lambda p:Q(** p),patterns))
filter_ids = Kid.objects.filter(filter_q).values_list('family__id',flat = True).distinct()
exclude_ids = Kid.objects.exclude(filter_q).values_list('family__id',flat = True).distinct()
parents = Parent.objects.filter(id _in = filter_ids).exclude(id__in = exclude_ids)
template ='index.html'
context = {'parents':parents}
返回渲染(请求,模板,上下文)

所以使用我当前的视图功能如上所示。所有的孩子都必须在1个父母中!!!



请帮助!

解决方案

如果我正确地理解你的问题,我将做什么。

  def index(request):
patterns = [
{'name':'samy','age__lt':15,'city':'paris'},
{'name':'sally','age__gt' 20,'city':'london'}
]
filter_q = reduce(operator.or_,map(lambda p:Q(** p),patterns))
qs = Kid。 object.filter(filter_q).values_list('id','family_id')
family_ids = set()
child_ids = list()
for child_id,family_id in qs:
family_ids.add(family_id)
child_ids.append(child_id)
#在这一点上,我们有child_ids
#中匹配的Kid的完整列表。 。 。我们有一个所有家庭ID的列表,其中存在这些孩子
#现在我们寻找不完整的家庭,
#ie,属于第一个查询的家庭
#但有孩子不在我们的完整列表
#匹配的孩子。
incomplete_family_ids = set(Kid.objects.exclude(id__in = child_ids).filter(family_id__in = family_ids).values_list('family_id',flat = True).distinct())
#现在我们找到设置补充使数据库上的生活更容易一些。
complete_family_ids = family_ids - incomplete_family_ids
parents = Parent.objects.filter(id__in = complete_family_ids)
template ='index.html'
context = {'parents':parents}
返回渲染(请求,模板,上下文)


what i need is to allow users to access list of Parent objects by filtering their related objects `Kid'

We have a dictionary that has a 100 kid. and parents that have about 5 kids in average.
if the kids in 1 parent object are all in the dictionary I want them to be listed. But not all 100 kid have to be related to the parent object.

e.g if the parent have 4 kids who are in the dictionary and 1 that is not. Then do not include them.

I have the code below

models.py :

class Parent(models.Model):
    title = models.CharField(max_length=250)
    address = models.CharField(max_length=250)


class Kid(models.Model):
    family = models.ForeignKey(Parent)
    name = models.CharField(max_length=250)
    age = models.IntegerField()
    city = models.CharField(max_length=250)

Views.py :

def index(request):
    patterns = [
        {'name': 'samy', 'age__lt': 15, 'city': 'paris'},
        {'name': 'sally', 'age__gt': 20, 'city': 'london'}
    ]
    filter_q = reduce(operator.or_, map(lambda p: Q(**p), patterns))
    filter_ids = Kid.objects.filter(filter_q).values_list('family__id', flat=True).distinct()
    exclude_ids = Kid.objects.exclude(filter_q).values_list('family__id', flat=True).distinct()
    parents = Parent.objects.filter(id__in=filter_ids).exclude(id__in=exclude_ids)
    template = 'index.html'
    context = {'parents': parents}
    return render(request, template, context)

so with my current view function shown above. all the kids have to be in 1 parent!!!

help please!

解决方案

Here is what I would do if I understood your question correctly.

def index(request):
    patterns = [
        {'name': 'samy', 'age__lt': 15, 'city': 'paris'},
        {'name': 'sally', 'age__gt': 20, 'city': 'london'}
    ]
    filter_q = reduce(operator.or_, map(lambda p: Q(**p), patterns))      
    qs = Kid.objects.filter(filter_q).values_list('id', 'family_id') 
    family_ids = set()
    child_ids = list()
    for child_id, family_id in qs:
        family_ids.add(family_id)
        child_ids.append(child_id)        
    # At this point we have the complete list of matching Kid(s) in child_ids
    # . . . and we have a list of all family IDs in which those Kid(s) exist
    # Now we look for incomplete families, 
    # i.e., families that were part of the first query, 
    # but have Kid(s) that are not in our complete list 
    # of matching kids.
    incomplete_family_ids = set(Kid.objects.exclude(id__in=child_ids).filter(family_id__in=family_ids).values_list('family_id', flat=True).distinct())
    # Now we find the set complement to make life on the database a little easier.
    complete_family_ids = family_ids - incomplete_family_ids
    parents = Parent.objects.filter(id__in=complete_family_ids)
    template = 'index.html'
    context = {'parents': parents}
    return render(request, template, context)    

这篇关于过滤对象,如果其所有外键都在字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆