如果对象的字段在视图psql django中只有特定值,如何查询该对象
how to query an object if it has a field with only a specific value in views psql django
我的models.py
是:
class Order(models.Model):
truck_number = models.CharField(max_length=30,default=None)
date= models.DateField()
product=models.CharField(max_length=30)
depot = models.CharField(max_length=10)
volume = models.CharField(max_length=30, blank=True)
volume_delivered = models.CharField(max_length=30, blank=True)
driver_name=models.CharField(max_length=30,default=None)
driver_id_number=models.IntegerField(default=None)
driver_phone_number=models.IntegerField(default=None)
order_status = models.CharField(max_length=50, blank=True)
所以我需要一个视图,如果只加载字段 order_status 的值,它需要呈现 Table Order 的整个对象。我如何在 Django 视图中编写此逻辑。
我设法通过使用做到了:
从django.db.models导入Q
然后使用 :
过滤视图
默认加载(请求):
loadings = Order.objects.filter(Q(order_status='Loaded') | Q(order_status='Released') | Q(order_status='Received'))
return render(request, 'loadings.html' ,{'loadings':loadings})
所以有两种方法,要么过滤你想要的,要么排除你不想要的。 (注意 __in 运算符的双下划线)
loadings = Object.objects.filter(order_status__in=['Loaded', 'Released', 'Received'])
或
loadings = Object.objects.exclude(order_status__in=['Failed', 'Error', 'Whatever'])
我的models.py
是:
class Order(models.Model):
truck_number = models.CharField(max_length=30,default=None)
date= models.DateField()
product=models.CharField(max_length=30)
depot = models.CharField(max_length=10)
volume = models.CharField(max_length=30, blank=True)
volume_delivered = models.CharField(max_length=30, blank=True)
driver_name=models.CharField(max_length=30,default=None)
driver_id_number=models.IntegerField(default=None)
driver_phone_number=models.IntegerField(default=None)
order_status = models.CharField(max_length=50, blank=True)
所以我需要一个视图,如果只加载字段 order_status 的值,它需要呈现 Table Order 的整个对象。我如何在 Django 视图中编写此逻辑。
我设法通过使用做到了:
从django.db.models导入Q 然后使用 :
过滤视图默认加载(请求):
loadings = Order.objects.filter(Q(order_status='Loaded') | Q(order_status='Released') | Q(order_status='Received'))
return render(request, 'loadings.html' ,{'loadings':loadings})
所以有两种方法,要么过滤你想要的,要么排除你不想要的。 (注意 __in 运算符的双下划线)
loadings = Object.objects.filter(order_status__in=['Loaded', 'Released', 'Received'])
或
loadings = Object.objects.exclude(order_status__in=['Failed', 'Error', 'Whatever'])