对不同的查询集进行过滤会拉出'distinct on'查询省略的值 - PostgreSQL

时间:2017-05-30 15:42:41

标签: django postgresql filter distinct

我有大量的车辆库存,我通过他们的型号清楚地检索。用户可以按条件(新建或已使用)过滤车辆。然而在后端,发生了一些奇怪的事情。不同的查询集的计数是683,并且条件只有两个可能的值,new和used。当过滤二手车时,我得到186,当过滤新车时,我总共得到521,707。我想“为什么新的数量和使用的数量相等于原始数量”。在进一步调试之后,我发现即使在对“model_no”进行'distinct'之后,新的过滤器也会在不同的查询中拉出具有型号的车辆。新过滤器是否会以某种方式弄乱'distinct on'

查看

def search_listings(request):
    # Start searching

    all_listings = Inventory.objects.all().distinct('model_no')



    # This is what I did to find out there were extra model numbers coming in 
    master_test_list = []
    master_model_no = []
    new_list = []

    new_listings = all_listings.filter(condition='N')
    used_listings = all_listings.filter(condition='U')

    for item in all_listings:
        if item.id not in master_test_list:
            master_test_list.append(item.id)
            master_model_no.append(item.model_no)

    for item in new_listings:
        if item.id not in master_test_list:
            new_list.append(item)

    for item in used_listings:
        if item.id not in master_test_list:
            new_list.append(item)

    for item in new_list:
        print('{} is in original model_no list = {}'.format(item.model_no, item.model_no in master_model_no))
        print('{} is in original id list = {}'.format(item.model_no, item.id in master_test_list))

    for param in request.GET:

        values = request.GET[param].split(',')

        # Splitting dash delimited values from fields such as make, sometimes brings more than what you were looking for
        # e.g. Land Pride brings up New Holland, because Holland has the world 'land' in it.
        count = 0
        for val in values:
            if '-' in val:
                values[count] = val.replace('-', ' ')
            count += 1

        if 'all' not in values:
            if param.lower() == 'years':
                all_listings = all_listings.filter(model_year__iregex=r'(' + '|'.join(values) + ')')
            elif param.lower() == 'condition':
                all_listings = all_listings.filter(condition__iregex=r'(' + '|'.join(values) + ')')
            elif param.lower() == 'location':
                all_listings = all_listings.filter(location__name__iregex=r'(' + '|'.join(values) + ')')
            elif param.lower() == 'type':
                all_listings = all_listings.filter(inv_type__name__iregex=r'(' + '|'.join(values) + ')')
            elif param.lower() == 'manufacturer':
                all_listings = all_listings.filter(make__name__iregex=r'(' + '|'.join(values) + ')')

    return all_listings

库存模型

class Inventory(models.Model):
    INVENTORY_STATUS = (
        ('A', 'Active'),
        ('S', 'Sold'),
        ('I', 'Inactive')
    )

    INVENTORY_CONDITION = (
        ('N', 'New'),
        ('U', 'Used')
    )
    parent = models.ForeignKey('self', null=True, help_text='Parent stock item CB_PORD column')
    inv_stock_no = models.CharField(max_length=100, unique=True, help_text='CB_ORD column')
    description = models.CharField(max_length=200, blank=True)
    status = models.CharField(max_length=10, choices=INVENTORY_STATUS)
    condition = models.CharField(max_length=2, choices=INVENTORY_CONDITION, help_text='CB_TYP column from inv file')
    location = models.ForeignKey(Location, related_name='inventory', null=True, blank=True, on_delete=models.SET_NULL)
    inv_type = models.ForeignKey(Inventory_Type, related_name='inventory')
    inv_subtype = models.ForeignKey(Inventory_Subtype, related_name='inventory')
    make = models.ForeignKey(Inventory_Make, related_name='inventory')
    model_no = models.CharField(max_length=50, blank=True, help_text='CB_MOD column from inv file')
    model_year = models.IntegerField(default=0, help_text='CB_MYR column from inv file')
    hours = models.IntegerField(default=0, help_text='CB_HRS column from inv file')
    serial_no = models.CharField(max_length=100, blank=True, help_text='CB_MSN column from inv file')
    original_cost = models.DecimalField(max_digits=10, decimal_places=2, help_text='CB_OCS column from inv file')
    original_price = models.DecimalField(max_digits=10, decimal_places=2, help_text='Calculated on original cost, can override')
    list_price = models.DecimalField(max_digits=10, decimal_places=2,
                                     help_text='Calculated on original cost, can override')

    general_info = models.TextField(max_length=10000, null=True, blank=True)

    is_featured = models.BooleanField(default=False)

    date_added_to_inv = models.DateField(blank=True, help_text='CB_DTA column')
    date_modified_in_inv = models.DateField(null=True, blank=True, help_text='CB_DTM')
    create_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now=True)

0 个答案:

没有答案