Model field's verbose_name from database?

57 views
Skip to first unread message

Jon Dufresne

unread,
Feb 4, 2014, 1:03:08 PM2/4/14
to django...@googlegroups.com
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

Me Sulphur

unread,
Feb 4, 2014, 11:42:35 PM2/4/14
to django...@googlegroups.com
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 = ''




Me Sulphur

unread,
Feb 4, 2014, 11:44:12 PM2/4/14
to django...@googlegroups.com
Correction: Values should also be strings

mapping = {
    'My Name': 'first_name',
    'Surname': 'last_name',
    'Personal Email': 'email'
}

Jon Dufresne

unread,
Feb 5, 2014, 12:47:09 PM2/5/14
to django...@googlegroups.com
> 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

Tom Evans

unread,
Feb 5, 2014, 1:50:42 PM2/5/14
to django...@googlegroups.com
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

Me Sulphur

unread,
Feb 5, 2014, 11:46:07 PM2/5/14
to django...@googlegroups.com
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.

Степан Дибров

unread,
May 28, 2014, 8:44:20 AM5/28/14
to django...@googlegroups.com
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 написал:
Reply all
Reply to author
Forward
0 new messages