【发布时间】:2020-02-29 19:37:22
【问题描述】:
我正在尝试列出用户的个人资料。我想以这样一种方式列出它们,即具有相同用户城市的个人资料应该首先出现,然后下一个优先级应该是州,然后是国家/地区,最后是其余的个人资料。这是我尝试过的。 型号
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(user=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() 相同
请帮助我。提前致谢
【问题讨论】:
标签: python django django-models orm django-queryset