Django queryset在多个模型上搜索,返回相同的对象

时间:2015-09-02 07:41:03

标签: django search django-models django-queryset

我尝试在我的网站上创建高级搜索,您正在查看与每个模型相关的各种模型,并始终返回符合某些参数的配置文件列表

以下是我的模特:

class Profile(models.Model):
    first_name=models.CharField(max_length=60, blank=False)
    last_name=models.CharField(max_length=60, blank=False)
    residence=models.CharField(max_length=60, null=True, blank=True)
    birthdate=models.DateField(null=True, blank=True)
    telephone=models.CharField(max_length=60, null=True, blank=True)
    email=models.EmailField(null=True, blank=True)
    linkedin=models.URLField(null=True, blank=True)
    starred=models.BooleanField(default=False)
    created_from = models.ForeignKey(EmployeeUser,  related_name='profile_author')
    created_on = models.DateField(default=tznow)
    internal_id = models.CharField(max_length=5, blank=True)

class Education(models.Model):
    almalaurea_id = models.CharField(max_length=60, null=True, blank=True)
    profile = models.ForeignKey(Profile, related_name='education_profile')
    education_type = models.ForeignKey(Education_type, related_name='education_type')

class Education_type(models.Model):
    VALUES = (
        (0, 'Altro'),
        (1, 'Licenza media'),
        (2, 'Diploma'),
        (3, 'Laurea Triennale'),
        (4, 'Laurea Magistrale'),
    )

    title = models.CharField(max_length=60)
    value = models.IntegerField(choices=VALUES)

我想搜索符合各种结果的个人资料,例如birthdateresidencestarrededucation(基于education_type) 这是一个示例场景,我的研究包括其他模型

这些是我认为的研究,我认为已经找到了两个查询的结果,我可以提取个人资料ID并进行比较,然后通过选择匹配的配置文件来运行另一个查询,但我认为它是'不是一个好主意,真实场景包括其他各种模型。

    filters_profile = []
    filters_education = []

    year = form.cleaned_data["year"]
    residence = form.cleaned_data["residence"]
    starred = form.cleaned_data["starred"]
    education_type = form.cleaned_data["education_type"]

    if year:
        filters_profile.append(Q(birthdate__year=year))

    if residence:
        filters_profile.append(Q(residence__icontains=residence))

    if starred:
        filters_profile.append(Q(starred=starred))

    result_profile = Profile.objects.filter(reduce(lambda q1, q2:  q1 & q2, filters_profile)).order_by('first_name')

    result_education = None
    if education_type:
        e = Education_type.objects.filter(title=education_type)
        result_education = Education.objects.filter(education_type=e).prefetch_related('profile','education_type')

有什么想法吗? 非常感谢提前:))

编辑:

关于@Geo Jacob的解决方案 这是第三个模型:

    if valutation:
        result_valutation = Status.objects.filter(valutation=valutation).values_list('profile_id', flat=True)
            key['id__in'] = result_valutation

为我的方案添加此代码,此解决方案不起作用,正如我在评论中所写的那样:)

"in practice, the content of key['id__in'] is overwritten when the other model query (this) is executed"

2 个答案:

答案 0 :(得分:1)

试试这个:

key = {}
year = form.cleaned_data["year"]
residence = form.cleaned_data["residence"]
starred = form.cleaned_data["starred"]
education_type = form.cleaned_data["education_type"]

if year:
    key['birthdate__year'] = year

if residence:
    key['residence__icontains'] = residence

if starred:
    key['starred'] = starred

if education_type:
    e = Education_type.objects.filter(title=education_type)
    result_education = Education.objects.filter(education_type=e).values_list('profile_id', flat=True)
    key['id__in'] = result_education

result_profile = Profile.objects.filter(**key).order_by('first_name')

答案 1 :(得分:0)

基于@Geo Jacob解决方案,我的解决方案可以处理2个以上的模型,谢谢

我进行检查,并将 m_bmparr.LoadBitmap(IDB_BITMAPARR); //bitmap is 144x48 (4 bit) m_imagelist.Create(48, 48, ILC_COLOR4, 0, 0); //3 * 48 = 144 m_imagelist.Add(&m_bmparr, RGB(255, 0, 255)); CBitmap* bitmap2; IMAGEINFO imgInfo; m_imagelist.GetImageInfo(1, &imgInfo); //Index 1 of imagelist bitmap2 = CBitmap::FromHandle(imgInfo.hbmImage); m_picture.SetBitmap(*bitmap2); //Show bitmap --> DOESN'T SHOW!! :( 仅匹配上一个查询中的ID,以便与结果相交

key['id__in']