Django model design: editable help text for individual model fields. Is there a foreign field that references a specific field of a model? -


i have several models several fields in app. want set way user able modify text system each field in model. can give me guidance on how design models, , field types use? don't feel right storing model , field name in charfields, if way, may stuck it.

is there more elegant solution using django?

for quick , silly example, app named jobs, 1 named fun, , make new app named helptext:

jobs.models.py:  class person(models.model):     first_name = models.charfield(max_length=32)     .     .     interests = models.textfield()     def __unicode__(self):         return self.name  class job(models.model):     name = models.charfield(max_length=128)     person = models.foreignkey(person)     address = models.textfield()     duties = models.textfield()     def __unicode__(self):         return self.name    fun.models.py:  class rollercoaster(models.model):     name = models.charfield(max_length=128)     scare_factor = models.positiveinteger()     def __unicode__(self):         return self.name  class bigdipper(rollercoaster):     max_elevation = models.positiveinteger()     best_comment_ever_made = models.charfield(max_length=255)     def __unicode__(self):         return super.name 

now, let's want have editable text on person.interests, , job.duties, rollercoaster.scare_factor, , bigdipper.best_comment_ever_made. i'd have like:

helptext.models.py:  django.contrib.contenttypes.models import contenttype  class helptext(models.model):     the_model = models.foreignkey(contenttype)     the_field = models.charfield(max_length=255)     helptext = models.charfield(max_length=128)     def __unicode__(self):         return self.helptext 

so, better way this, other making helptext.the_model , helptext.the_field charfields have compared when rendering template see if helptext associated each field on screen?

thanks in advance!

edit:

i know help_text parameter of fields, want edited through gui, , may contain lot of styling, etc. html upwards of 50-60 lines of text 100 different model fields. don't want store in field definition reasons.

i changed helptext model have reference contenttype , field charfield. seem solution? not sure elegant way. please advise.

edit 2013-04-19 16:53 pst:

currently, tried , works, not sure great:

from django.db import models django.contrib.contenttypes.models import contenttype  # field choices drop down. fields = ()  # each contenttype verify model_class() not none , if not, add tuple # fields model name , field name displayed, storing field # name. ct in contenttype.objects.all():     m = ct.model_class()     if m not none:         f in ct.model_class()._meta.get_all_field_names():             fields += ((f, str(ct.model) + '.' + str(f)),)   # helptext model, associated multiple models , fields. class helptext(models.model):     the_model = models.foreignkey(contenttype)     the_field = models.charfield(max_length=255, choices=fields)     helptext = models.textfield(null=true, blank=true)      def __unicode__(self):         return self.helptext 

doesn't feel best, please advise if solution bite me in behind later on , make me filled regrets... :*(

the solution works, , have implemented, have aware contenttypes out of sync models. can manually update content types this:

python manage.py shell >>> django.contrib.contenttypes.management import update_all_contenttypes >>> update_all_contenttypes(interactive=true) 

this allows add new ones , remove old ones, if exist.

the nice thing field not being foreign key can put in text. so, have field "first name." can put helptext connected person model , "first_name" field. can make up, "something confusing." helptext associated person model , "something confusing" field. so, can put @ top of form, instead of associating field hard foreign keying. can arbitrary , follow "field" anywhere. hangup may change name of helptext field association inadvertently sending original helptext never land.

to make easy, created templatetag, pass name of model , name of "field" want associate. anytime template rendered, helptext there, editable assistance user interface forms.

not sure best solution, couldn't see other way it, , got no responses.

cheerio!


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -