过滤后重复结果

时间:2019-06-20 13:14:51

标签: python django

当我按idfirst_namelast_name进行过滤时,会得到重复的结果。 如果将.distinct()放在filter()之后,则会得到比以前更多的重复结果。似乎是involved__user__关系。

order = request.GET.get('order_by','orderDate')
jobs = Job.objects.order_by(order)

            if _filter:
                jobs = jobs.filter(Q(bundle__icontains=_filter) | 
                                Q(onBehalfOf__icontains=_filter) | 
                                Q(involved__user__first_name__icontains=_filter) | 
                                Q(involved__user__last_name__icontains=_filter) | 
                                Q(involved__user__email__icontains=_filter) | 
                                Q(description__icontains=_filter) | 
                                Q(id__iexact=_filter))

有了distinct(),我得到的重复结果比没有它的要多:

order = request.GET.get('order_by','orderDate')
jobs = Job.objects.order_by(order)

        if _filter:
            jobs = jobs.filter(Q(bundle__icontains=_filter) | 
                            Q(onBehalfOf__icontains=_filter) | 
                            Q(involved__user__first_name__icontains=_filter) | 
                            Q(involved__user__last_name__icontains=_filter) | 
                            Q(involved__user__email__icontains=_filter) | 
                            Q(description__icontains=_filter) | 
                            Q(id__iexact=_filter)).distinct()

我的模特是:

class Job(models.Model):
    involved = models.ManyToManyField(UserProfile, through="Membership")
    ...

class Membership(models.Model):
    user = models.ForeignKey(UserProfile)
    job = models.ForeignKey(Job)
    ...

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    ...

0 个答案:

没有答案