Tuesday, April 28, 2015

Re: forcing user model to return first_name last_name if exists

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCAAGBQJVP6mKAAoJEC0ft5FqUuEh6eYP/0fGpBciYS+zJ+8k05tg0tdC
36nCDwZItua8mf2W7Yp5eXVxnBexPnpQO2IzC/Xz/kR7m3ZLWEzPezmPTpjCOfEi
5FNq6wgQgCIpw02P+d6qg3HnsMq1cV5ABX5FDFEMkfMNFnyeFvyV5IGF3/HvbcmO
FP/Kdns2kJ05kO6u2HEQHuhOIVy829uZP91AHVpuzUQs262aeDUJDAexA6NYScsE
MWouhFK0VMc/Ndy2ZYUSi+qCZYwSQHOJpnxC7oZ/AKM4ciWSRTua5IFScSmRKDOa
BzooWVJiqWJMw3kOB0p92jZfWKK/vgO/tOB7LM+/abTksqn2gAV9FdIgMYlOMKxb
afxGapWw6XyiU6HhbPG5mZgODirv9CRkDd3gsJZ50EI/VNgDRl3oRPIn1VnfEqn3
MOUkA16FcsmPCSiZFu5iu3SCEa700M7jnFdvr8JfVaSeFrU+1MstdMKnUa2zZMTm
lGUqujT+yBDP3XSr7SIiA4pljbAnGjACrXkcUWYgvF885JFBOiKmDrn8DniU+I3z
R39eDCsiD/3vpQkG45w8powjAUnrltIShEX8NFXssT3DQS9qQVUG6BRDpeHSieiw
qtNIcbKKl2DoNuTFFIbxcj7m5CGHG8aNrjoZkhEc0bSGRsfwq86nZKZnPymDbbOW
BO/KiaIE4mRoSC95f75C
=6hzU
-----END PGP SIGNATURE-----
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/553FA98A.4060301%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment