forcing user model to return first_name last_name if exists

閲覧: 18 回
最初の未読メッセージにスキップ

Ilya Kazakevich

未読、
2015/04/28 11:10:312015/04/28
To: django...@googlegroups.com
Hello,

I have many places in my app where user (from user model) is displayed: templates, forms, fliters, tables etc.
It is displayed as username everywhere.

I want it to be displayed as first_name/last_name.

I can do that with monkey patching:

@receiver(request_started)
def patch(*args, **kwargs):
    get_user_model().__str__ = my_smart_str_func

I use "request_started" to make sure all models are loaded. (BTW, does there is something like "models loaded" signal?).

How ever, I feel it a little bit hacky. There should be more pythonic way to do that. Am I miss something?

Carl Meyer

未読、
2015/04/28 11:39:132015/04/28
To: django...@googlegroups.com
On 04/28/2015 09:10 AM, Ilya Kazakevich wrote:
> I have many places in my app where user (from user model) is displayed:
> templates, forms, fliters, tables etc.
> It is displayed as username everywhere.
>
> I want it to be displayed as first_name/last_name.
>
> I can do that with monkey patching:
>
> @receiver(request_started)
> def patch(*args, **kwargs):
> get_user_model().__str__ = my_smart_str_func
>
> I use "request_started" to make sure all models are loaded. (BTW, does
> there is something like "models loaded" signal?).

Not a signal; the proper place for code like this in Django 1.7+ is in
an AppConfig.ready() method.

> How ever, I feel it a little bit hacky. There should be more pythonic
> way to do that. Am I miss something?

Of course, the best way to do this is a custom User model so you don't
have to monkeypatch at all. Sadly, switching to a custom User model is
difficult if the project didn't start with one.

Carl

signature.asc
全員に返信
投稿者に返信
転送
新着メール 0 件