forcing user model to return first_name last_name if exists

18 views
Skip to first unread message

Ilya Kazakevich

unread,
Apr 28, 2015, 11:10:31 AM4/28/15
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

unread,
Apr 28, 2015, 11:39:13 AM4/28/15
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
Reply all
Reply to author
Forward
0 new messages