[英]How do I sort the user model by fields in the profile model (1: 1)?
skill_note_reputation is a field in the Profile model that has a 1: 1 relationship with Profileuser. Skill_note_reputation 是 Profile model 中的一个字段,与 Profileuser 具有 1:1 的关系。
How do I sort user models by skill_note_reputation values?如何按 Skill_note_reputation 值对用户模型进行排序?
user_list = User.objects.all ()
Thanks for letting me know how to fix it感谢您让我知道如何解决它
If you add the parent_link=True
key to your OneToOneField
, you can reference fields in the Profileuser
as if they belonged to the User
model.如果将parent_link=True
键添加到OneToOneField
,则可以引用Profileuser
中的字段,就好像它们属于User
model 一样。
For example:例如:
class Profileuser(models.Model):
user = models.OneToOneField(
User, on_delete=models.CASCADE,
related_name='profileuser', parent_link=True)
skill_note_reputation = models.IntegerField()
Now, you can filter your User
model like this:现在,您可以像这样过滤您的User
model:
User.objects.filter(skill_note_reputation=1)
For a more pythonic approach, you can take advantage of Django's model inheritance , and turn your Profileuser
model into this:对于更Pythonic的方法,您可以利用 Django 的model inheritance ,并将您的Profileuser
model 变成这样:
class Profileuser(User):
skill_note_reputation = models.IntegerField()
Note that this model and the above model are identical.注意这个model和上面的model是一样的。
If for some reason you don't want to do use parent_link=True
, you can reference any ForeignKey
columns using Django's LOOKUP_SEP
, like this:如果出于某种原因您不想使用parent_link=True
,您可以使用 Django 的LOOKUP_SEP
引用任何ForeignKey
列,如下所示:
User.objects.filter(profileuser__skill_note_reputation=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.