如何在 Django 中按特定顺序构建查询集

How to build a queryset in a specific order in Django

我正在尝试列出用户的个人资料。我想以这样的方式列出它们:具有相同用户城市的配置文件应该排在第一位,然后下一个优先级应该是州,然后是国家,最后是其余配置文件。这是我试过的。 型号

class Profile(models.Model):
    uuid = UUIDField(auto=True)
    user = models.OneToOneField(User)
    country = models.ForeignKey(Country, null=True)
    state = models.ForeignKey(State, null=True)
    city = models.ForeignKey(City, null=True)

views.py current_user = Profile.objects.filter(用户=request.user)

profiles_city = Profile.objects.filter(city=current_user.city)
profiles_state = Profile.objects.filter(state=current_user.state)
profiles_country = Profile.objects.filter(country=current_user.country)
profiles_all = Profile.objects.all()
profiles = (profiles_city | profiles_state | profiles_country | profiles_all).distinct()

但它产生的结果与 Profile.objects.all()

相同

请帮帮我。提前致谢

您需要 QuerySetorder_by 方法,它根据传递的参数对对象进行排序;这是在数据库上完成的:

Profile.objects.order_by(
    'current_user__city',
    'current_user__state',
    'current_user__country',
)

编辑:

如果您想按登录用户的 citystatecountry 名称排序,您可以在 Python 级别执行此操作,使用sorted,以及自定义 key 可调用:

from functools import partial


def get_sort_order(profile, logged_in_profile):
    # This is a simple example, you need to filter against
    # the city-state-country combo to match precisely. For
    # example, multiple countries can have the same city/
    # state name.

    if logged_in_profile.city == profile.city: 
        return 1 
    if logged_in_profile.state == profile.state: 
        return 2 
    if logged_in_profile.country == profile.country: 
        return 3
    return 4 

logged_in_profile = request.user.profile  # logged-in user's profile
get_sort_order_partial = partial(get_sort_order, logged_in_profile=logged_in_profile)

sorted(
    Profile.objects.all(),
    key=get_sort_order_partial,
)

在数据库级别做同样的事情,使用 CaseWhen 得到 Python if-elif-else喜欢构造:

from django.db.models import Case, When, IntegerField

Profile.objects.order_by( 
    Case( 
        When(city=logged_in_profile.city, then=1), 
        When(state=logged_in_profile.state, then=2), 
        When(country=logged_in_profile.country, then=3), 
        default=4, 
        output_field=IntegerField(), 
    ) 
)         

这将产生一个查询集,并且还具有更快的额外优势,因为所有操作都将在数据库上完成 (SELECT CASE WHEN ...)。