简体   繁体   English

Django 过滤对象 id 并在用户 model 中搜索全名

[英]Django filter objects id and search for the full name in User model

I have a problem of solving this for the whole day and can't get it right.我整天都在解决这个问题,但无法正确解决。 Please someone help me...请有人帮我...

book_obj = Book.objects.filter(status_sold=True).order_by('-date_sold') book_obj = Book.objects.filter(status_sold=True).order_by('-date_sold')

result will be something like this:结果将是这样的:
id = 2编号 = 2
title = 'bla bla bla bla...'标题 = 'bla bla bla bla ...'
author = 3 ---> id from the User model (not a foreign key) author = 3 ---> 来自用户 model 的 id(不是外键)
status_sold = True status_sold = 真
date_sold = '2021-05-10' date_sold = '2021-05-10'

I want to view that data in the template, but the author I want to display the name instead of number 3 as an id of the author.我想在模板中查看该数据,但作者我想显示名称而不是数字 3 作为作者的 id。 How to solve this?如何解决这个问题?

author_name = User.objects.get(id=???????? ) author_name = User.objects.get(id=?????????? )

so I can use {{ author_name.get_full_name }} later in the template所以我可以稍后在模板中使用{{ author_name.get_full_name }}

Thx in advanced....谢谢先进....

Use this in a template.在模板中使用它。 The author user object is already attached to the book object so you don't need to fetch it from the database again.作者用户 object 已经附在书 object 中,因此您无需再次从数据库中获取它。

{{ book_obj.author.get_full_name }}

Another option is to set the __str__ method on the user object to另一种选择是将用户 object 上的__str__方法设置为

def __str__(self):
    return self.get_full_name

Then in the template you can just do然后在模板中你可以做

{{ book_obj.author }}

If you show a bit more of your models, it may be easier to provide a more concrete answer, but I think we've got enough to give some help.如果您展示更多模型,提供更具体的答案可能会更容易,但我认为我们已经足够提供一些帮助。

In your template, where you have author like:在您的模板中,您的作者如下:

{{author}}

you can simply add on the property you want with dot notation:你可以简单地用点符号添加你想要的属性:

{{author.full_name}}

or what ever you put for the field containing the full name.或者您为包含全名的字段输入的内容。

No need to separately pass a user object in your context.无需在您的上下文中单独传递用户 object。 This way works well in loops as well.这种方式在循环中也很有效。

Link to documentation: https://docs.djangoproject.com/en/3.2/topics/templates/#variables文档链接: https://docs.djangoproject.com/en/3.2/topics/templates/#variables

EDIT: apologies - missed the no foreign key bit.编辑:道歉 - 错过了没有外键位。

you may be looking at custom template tags.您可能正在查看自定义模板标签。

from django.template.defaulttags import register

@register.filter
def get_author_name(key):
    author = Author.objects.get(pk=key)
    return author.full_name

then use it in template like: {{ author|get_author_name }}然后在模板中使用它: {{ author|get_author_name }}

You could probably jazz it up a bit and add an attribute as an arg.您可能会对其进行一些改进并添加一个属性作为 arg。 See: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters请参阅: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters

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

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