Re: Model field's verbose_name from database?
from django.utils.functional import lazy def field_verbose_db(field_name): from app.models import FieldNamesMap return FieldNamesMap.objects.get(key=field_name).title field_verbose_db_lazy = lazy(field_name, unicode) first_name = models.CharField(..., verbose_name=field_verbose_db_lazy('first_name')) вторник, 4 февраля 2014 г., 22:03:08 UTC+4 пользователь Jon Dufresne написал: > > Hi, > > I have a model with fields that allows the user to modify the > user-facing names of these fields. Think "first name", "last name", > "email", the user may prefer these be displayed as "given name", > "surname", "email address". This is configured exactly once throughout > the system. So in this example *all* instances of "first name" should > instead be displayed as "given name". Is it possible have a model > where "verbose_name" is not a static string, but instead comes from > the database? Perhaps with some lazy loading trickery? I want to do > something in the spirit of: > > first_name = models.CharField(..., verbose_name=lazy_load_name_from_db()) > > My hope is to eliminate the need to think about what to call this > field when displayed to the user. > > Thanks, > Jon > -- 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+unsubscr...@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/18b0c5bc-71c3-42d7-841e-3afd13c0bd71%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Model field's verbose_name from database?
You could easily convert mappings to JSON and store it in a model like: Class VerboseNameMaps(models.Model): map = models.TextField() user = models.ForeignKey(User) When you wish to use the same, you could: map_obj = VerboseNameMaps.objects.get(user=request.user) mapping = json.loads(map_obj.map) # Now you can use getattr() along with mapping as in my earlier mail You can create a forms or interfaces for user to Create, Read, Update and Delete (CRUD) these maps, so user need not write python files. -- 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+unsubscr...@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/CABSvzZDPeTMxwRAtfP7mMn%2B2PLgumhopDUdEz_-swETVS4Z96A%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Model field's verbose_name from database?
On Wed, Feb 5, 2014 at 5:47 PM, Jon Dufresnewrote: >> Why not use dictionary to Map user-level names to database-fields > > I'm not sure I fully follow what you're suggesting. But it sounds like > you're suggesting I move the configuration of field name out of the > database and into a Python file using a dictionary to map actual > fields to preferred field names. Is this correct? > > I can't do this because: > > * I do not know the user's preference ahead of time > * Their preference could change as time goes on > * It is unreasonable (in my case) to expect my users to modify python files > * These preferences could be different across different installations > > For this reason, these fields are configured as a sort of "user > setting". The user is in complete control. But, once they are set, > they should be used everywhere. Do the names of the fields differ for different installation instance, or do they differ for different users on the same installation? If the former, I would do it with translations. Combined with something like django-rosetta, you can easily knock up an interface to allow super-users to set the translation for "First name" to whatever they want by translating the "en" language (or indeed, any language). If your site is multi-lingual already, this should be quite easy (and frankly, if its not, its also very easy to configure). verbose_name will happily accept a lazy translation as a value, converting it on display to the "language" specified . If the latter, then I don't think you can hammer this in to the verbose_name mechanism without becoming very inventive - perhaps still with translations, activating a different .mo file per-user? Cheers Tom -- 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+unsubscr...@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/CAFHbX1LkRG1_0oDepzJO%2B_LaTeK%2BCyLN3KnwSh1tQ1pj-ZO22Q%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Model field's verbose_name from database?
> Why not use dictionary to Map user-level names to database-fields I'm not sure I fully follow what you're suggesting. But it sounds like you're suggesting I move the configuration of field name out of the database and into a Python file using a dictionary to map actual fields to preferred field names. Is this correct? I can't do this because: * I do not know the user's preference ahead of time * Their preference could change as time goes on * It is unreasonable (in my case) to expect my users to modify python files * These preferences could be different across different installations For this reason, these fields are configured as a sort of "user setting". The user is in complete control. But, once they are set, they should be used everywhere. Cheers, Jon -- 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+unsubscr...@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/CADhq2b7%3D_EmMiQtpnLVHUOSPY3wvjBsdiKF%2BbP2WHL%3DhJocvNg%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Model field's verbose_name from database?
Correction: Values should also be strings mapping = { 'My Name': 'first_name', 'Surname': 'last_name', 'Personal Email': 'email' } -- 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+unsubscr...@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/d9406eda-388d-41d2-a488-c39862e6ea0f%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Model field's verbose_name from database?
Why not use dictionary to Map user-level names to database-fields mapping = { 'My Name': first_name, 'Surname': last_name, 'Personal Email': email } u = User.objects.get(pk=1) key = 'My Name' try: val = getattr(u, mapping[key]) except AttributeError: val = '' -- 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+unsubscr...@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/1ed11234-acfb-4d7a-b67e-631e583dc51b%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Model field's verbose_name from database?
Hi, I have a model with fields that allows the user to modify the user-facing names of these fields. Think "first name", "last name", "email", the user may prefer these be displayed as "given name", "surname", "email address". This is configured exactly once throughout the system. So in this example *all* instances of "first name" should instead be displayed as "given name". Is it possible have a model where "verbose_name" is not a static string, but instead comes from the database? Perhaps with some lazy loading trickery? I want to do something in the spirit of: first_name = models.CharField(..., verbose_name=lazy_load_name_from_db()) My hope is to eliminate the need to think about what to call this field when displayed to the user. Thanks, Jon -- 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+unsubscr...@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/CADhq2b6hoXB66hE2oWkhXe4TsvNmfWE%2Bw72dHj2ipkyYRGv1bw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.