【问题标题】:django foreignkey - format output value in selectlistdjango foreignkey - 选择列表中的格式输出值
【发布时间】:2014-08-12 18:29:00
【问题描述】:

表单生成所有可用外键元素的列表。它们的显示取决于模型中定义的__str__ 函数。

我不知道如何搜索这个,但需要在不接触模型的情况下更改它。

我的模型与用户模型连接。此模型显示用户名,但我想调用 _get_full_name 函数以获得更好的列表。

class Trainer(models.Model):
    user=models.OneToOneField(User)

是否可以在外键的定义内或连接的模型内(在我的示例中为 Trainer 模型)定义它?

【问题讨论】:

    标签: python django django-models representation generic-foreign-key


    【解决方案1】:

    这应该可行:

    def user_new_unicode(self):
        return self.get_full_name()
    User.__unicode__ = user_new_unicode #or User.__str__ = user_new_unicode
    

    当然应该放在你的班级里。

    【讨论】:

    • 因为它有效,请将此答案标记为正确以供将来参考。
    猜你喜欢
    • 2021-02-26
    • 2013-02-05
    • 2012-10-16
    • 1970-01-01
    • 2014-03-20
    • 2022-11-12
    • 2014-06-30
    • 2014-12-10
    • 2010-09-22
    相关资源
    最近更新 更多