简体   繁体   English

如何在django的ORM中的一对多关系中搜索字段的模式?

[英]How to search for the mode of a field in a one to many relationship in django's ORM?

I have this one to Many relationship between dish and restaurant.我有这道菜和餐厅之间的一对多关系。

SPICINESS_CHOICES = (
    ("M", "Mild")
    ("H", "Hot")
    ("SH", "Super hot")
)
class Restaurant(Model):
    name = models.CharField(max_length=50)



class Dish(Model):
    restaurant = models.ForeignKey(Restaurent)
    name = models.CharFiedl(null=True, max_length=50)
    price = models.IntegerField(max_length=10, null= False)
    spicy = models.CharField(null= True, choices=SPICINESS_CHOICES)

I would like to be able to search Restaurants for the most occurring spiciness of their dishes or the mode of the spiciness of their dishes https://en.wikipedia.org/wiki/Mode_(statistics) .我希望能够在餐馆中搜索最常见的菜肴辣味或菜肴的辣味模式https://en.wikipedia.org/wiki/Mode_(statistics) Django does not seem to have functionality to implement this easily. Django 似乎没有轻松实现这一点的功能。 So let's say we have three restaurants, Anny's, Domino and Verspucie.假设我们有 3 家餐厅,Anny's、Domino 和 Verspucie。

Anny's has different 8 dishes of which 2 dishes are labeled as being Hot, all other dishes are labeled as Mild, since the owner wants to be able to have a varied menu for children Anny's 有不同的 8 道菜,其中 2 道菜被标记为热的,所有其他菜都被标记为温和的,因为主人希望能够为孩子们提供多样化的菜单

Domino has 9 different dishes of which 5 dishes are labeled as being Hot, 2 as being Super Hot and 2 as being mild. Domino 有 9 种不同的菜肴,其中 5 道菜被标记为“热”,2 道被标记为超级热,2 道被标记为温和。

Verspucie has 10 different dishes of which 8 are labeled as being Super Hot, one 1 labeled as Mild and 1 is labeled as Hot. Verspucie 有 10 种不同的菜肴,其中 8 种被标记为超级热,1 种被标记为温和,1 种被标记为热。

Because I want to find the correct restaurant from a whole list of restaurants, I would like to be able to search the restaurants for their most occuring spiciness in their dishes.因为我想从整个餐厅列表中找到正确的餐厅,所以我希望能够搜索餐厅的菜肴中最常见的辣味。 In postgres one could do this using the mode function and then joining this mode value on to the restaurants table.在 postgres 中,可以使用 mode 函数来做到这一点,然后将此 mode 值加入到餐厅表中。 If I would want to have a restaurant which mostly has Super Hot dishes my query would look something like this:如果我想要一家以超级热菜为主的餐厅,我的查询将如下所示:

SELECT * FROM "restaurant" LEFT OUTER JOIN (SELECT "dish"."restaurant_id" ,mode() WITHIN GROUP (ORDER BY "dish.spicy") AS "mode_spicy" FROM dish GROUP BY "dish"."restaurant_id") mode_table ON "id"="mode_table"."taxonomy_id") WHERE "mode_spicy"='SH';

This would then yield the Verspucie restaurant since it is the only restaurant which has most of its dishes labeled as Super Hot.这将产生 Verspucie 餐厅,因为它是唯一一家将大部分菜肴标记为 Super Hot 的餐厅。 But now I would like to do this in the django ORM langauge yet I can't find a mode() function in django.但是现在我想在 django ORM 语言中执行此操作,但在 django 中找不到 mode() 函数。

Edit: I went for the SQL query in the end编辑:我最后去了 SQL 查询

    from django.db.models import Count, IntegerField, When, Case
    Restaurant.objects.annotate(
        numcount=Count(Case(
            When(dish__spicy="SH", then=1),
            output_field=IntegerField(),
        ))
    ).order_by("-numcount")

this query will count number of dish have spicy SH each restaurant, And you can order this, checking after this for find restaurant have most SH with your logic此查询将计算每家餐厅有辛辣SH的菜数,您可以SH ,然后根据您的逻辑检查查找餐厅有最多SH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM