Django make model field name a link

what i want to do is have a reference to the name of the model field. So when I fill out the form using the admin interface, I can access some information.

I know it doesn't work, but it shows what I want to do

class A(models.Model):
    item_type = models.CharField(max_length=100, choices=ITEMTYPE_CHOICES, verbose_name="<a href='http://www.quackit.com/html/codes'>Item Type</a>")

      

Another option is to place a description next to the field.

I don't even know where to start.

+2


a source to share


2 answers


Unfortunately, this is much more difficult than at first glance. There is a lot of manipulation going on between the point you define verbose_name

and the time it gets to {{ field.label_tag }}

in the template admin/includes/fieldset.html

. Essentially no matter what you do, the string is forced back to unicode and (ultimately) escaped to label_tag

. Attempting to use templates mark_safe

or SafeUnicode

or even |safe

filters fails to prevent escaping.

This means you have three options:



  • Make a lot of hack inside django internals to carry the SafeUnicode string to the end without saving.

  • Manually create a field label tag in the template admin/includes/fieldset.html

    . Be aware that there are many important attributes on this label like id, for, class, etc.

  • Create a templating filter that parses the string inside the tag tag and converts it to a link for you.

Option three might be the easiest if you're not good at regexes at all.

+1


a source


This is what really needs to be handled in the template.

Here's one way to do it ...

You can create a whole other model called Description, and then create an Item Type as an entry in this table.

From there you can check out the permalink decorator http://docs.djangoproject.com/en/dev/ref/models/instances/#the-permalink-decorator which will allow you to create permalinks to any given Description object.



Finally, take a look at http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#overriding-admin-templates for changing admin templates. Wherever you edit objects class A

, you can paste the anchor into the permalink of that Description object.

What you want to do is definitely possible. You just need to reorganize your thinking.

Good luck!

0


a source







All Articles