Django 选择一个计数
Django selecting a count
我是 Django 的新手,我想获得用户拥有的客户总数(我的用户将通过我的网站销售东西)所以我有一个 table 称为订单,我将 user_id
购买的用户和 product_id
正在购买的用户。每个产品 ID 都与具有用户(我正在查询的用户)的产品相关:
select COUNT(distinct(o.user_id)) as total_clients from `order` o
inner join product p on p.id=o.product_id where p.user_id=32;
用户 ID 32
已登录,我想告诉他有多少客户购买了他的产品。
我想在 get
而不是 filter
中执行此操作,因为它更有意义。
这是我的逻辑告诉我要写的内容:
clients = Order.objects.get(
status = Order.COMPLETED,
product__user = self.user
).annotate(
total_clients = Count( 'user', distinct = True )
)
return clients.total_clients
.这就是它 returns:
Exception Type: MultipleObjectsReturned
Exception Value: get() returned more than one Order -- it returned 2!
我可能会 运行 查询而不是使用 orm,但我不想那样。这是一个相当简单的查询,我确信 Django 处理起来非常容易,我想避免在我的代码中写入字符串。
这是我的模型设置:
class UserProfile( models.Model ):
user = models.OneToOneField( User, related_name = 'profile' )
....
def get_total_clients( self ):
clients = Order.objects.get(
status = Order.COMPLETED,
product__user = self.user
).annotate(
total_clients = Count( 'user', distinct = True )
)
return clients.total_clients
class Order( models.Model ):
PENDING = 0
COMPLETED = 1
REFUNDED = 2
STATUS_CHOICES = (
(PENDING, "Pending"),
(COMPLETED, "Completed"),
(REFUNDED, "Refunded"),
)
user = models.ForeignKey( User, related_name = "orders" )
product = models.ForeignKey( Product, related_name = "orders" )
amount = models.DecimalField( max_digits = 6, decimal_places = 2, default = 0.00 )
status = models.SmallIntegerField(
choices = STATUS_CHOICES, default = PENDING, db_index = True
)
class Product( models.Model ):
user = models.ForeignKey( User, related_name = "unlocks" )
name = models.CharField( max_length = 255 )
Django 查询集有一个 count 方法:
clients = Order.objects.filter(
status = Order.COMPLETED,
product__user = self.user
)
return clients.count()
如果我没听错,您感兴趣的是从用户那里订购产品的消费者数量。一些 docs,可能会有帮助。
我的建议是:
result = Product.objects.distinct().filter(
# or any query:
orders__status=Order.COMPLETED,
user=default_user
).aggregate( # see the difference between aggregate() and annotate()
total_users=Count('orders__user', distinct=True)
)
我希望结果是:{'total_users': NUM_OF_USERS}
在原始 SQL 中,它将类似于:
SELECT DISTINCT COUNT(DISTINCT "main_order"."user_id") AS
"total_users" FROM "main_product" INNER JOIN "main_order" ON (
"main_product"."id" = "main_order"."product_id" ) WHERE
("main_order"."status" = STATUS AND "main_product"."user_id" = USER_ID)
这是你想要的吗?
我是 Django 的新手,我想获得用户拥有的客户总数(我的用户将通过我的网站销售东西)所以我有一个 table 称为订单,我将 user_id
购买的用户和 product_id
正在购买的用户。每个产品 ID 都与具有用户(我正在查询的用户)的产品相关:
select COUNT(distinct(o.user_id)) as total_clients from `order` o
inner join product p on p.id=o.product_id where p.user_id=32;
用户 ID 32
已登录,我想告诉他有多少客户购买了他的产品。
我想在 get
而不是 filter
中执行此操作,因为它更有意义。
这是我的逻辑告诉我要写的内容:
clients = Order.objects.get(
status = Order.COMPLETED,
product__user = self.user
).annotate(
total_clients = Count( 'user', distinct = True )
)
return clients.total_clients
.这就是它 returns:
Exception Type: MultipleObjectsReturned
Exception Value: get() returned more than one Order -- it returned 2!
我可能会 运行 查询而不是使用 orm,但我不想那样。这是一个相当简单的查询,我确信 Django 处理起来非常容易,我想避免在我的代码中写入字符串。
这是我的模型设置:
class UserProfile( models.Model ):
user = models.OneToOneField( User, related_name = 'profile' )
....
def get_total_clients( self ):
clients = Order.objects.get(
status = Order.COMPLETED,
product__user = self.user
).annotate(
total_clients = Count( 'user', distinct = True )
)
return clients.total_clients
class Order( models.Model ):
PENDING = 0
COMPLETED = 1
REFUNDED = 2
STATUS_CHOICES = (
(PENDING, "Pending"),
(COMPLETED, "Completed"),
(REFUNDED, "Refunded"),
)
user = models.ForeignKey( User, related_name = "orders" )
product = models.ForeignKey( Product, related_name = "orders" )
amount = models.DecimalField( max_digits = 6, decimal_places = 2, default = 0.00 )
status = models.SmallIntegerField(
choices = STATUS_CHOICES, default = PENDING, db_index = True
)
class Product( models.Model ):
user = models.ForeignKey( User, related_name = "unlocks" )
name = models.CharField( max_length = 255 )
Django 查询集有一个 count 方法:
clients = Order.objects.filter(
status = Order.COMPLETED,
product__user = self.user
)
return clients.count()
如果我没听错,您感兴趣的是从用户那里订购产品的消费者数量。一些 docs,可能会有帮助。
我的建议是:
result = Product.objects.distinct().filter(
# or any query:
orders__status=Order.COMPLETED,
user=default_user
).aggregate( # see the difference between aggregate() and annotate()
total_users=Count('orders__user', distinct=True)
)
我希望结果是:{'total_users': NUM_OF_USERS}
在原始 SQL 中,它将类似于:
SELECT DISTINCT COUNT(DISTINCT "main_order"."user_id") AS
"total_users" FROM "main_product" INNER JOIN "main_order" ON (
"main_product"."id" = "main_order"."product_id" ) WHERE
("main_order"."status" = STATUS AND "main_product"."user_id" = USER_ID)
这是你想要的吗?