Django ORM查询

皮疹77

我是新来的djangoSQL太。我需要列出一个价格便宜的供应商产品清单,然后product_id从其他供应商处以相同的价格登录(要求相同)。要求:Django=2.2 PostgreSQL=9.6让我知道如何使用django-ORM,以及仅使用SQL语言编写的代码是什么这是模型。

Models.py
class Product(models.Model):
    name = models.CharField('product name', max_length=50)
    product = models.CharField('vendor code', default=None, max_length=50, unique=True)

class Supplier(models.Model):
    name = models.CharField('Supplier name', default='', max_length=50)

Class SupplierProduct(models.Model): 
    supplier = models.ForeignKey(Supplier, default=None, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, default=None, on_delete=models.CASCADE)
    product_price = models.DecimalField('Price', default=None, max_digits=11, decimal_places=2)
    availability = models.BooleanField(default=False)

Views.py
def foo(request):
    user = request.user
皮疹77
SupplierProduct.objects.annotate(
    has_cheaper=Exists(SupplierProduct.objects.filter(
        Q(supplier__name=user),
        ~Q(supplier_id=OuterRef('supplier_id')),
        product_id=OuterRef('product_id'),
        product_price__gt = OuterRef('product_price')
        )
    )
).filter(availability=True, has_cheaper=True)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章