开发者

Django make model field name a link

开发者 https://www.devze.com 2022-12-30 11:36 出处:网络
what Im looking to do is to have a link on the name of a field of a model. So when im filling the form using the admin interface i can access some information.

what Im looking to do is to have a link on the name of a field of a model. So when im filling the form using the admin interface i can access some information.

I know this doesn't work but 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.co开发者_如何学编程m/html/codes'>Item Type</a>")

Other option would be to put a description next to the field.

Im not even sure where to start from.


Sadly, this is a lot harder than at first glance. Between the point at which you define verbose_name and the time it gets to {{ field.label_tag }} in the admin/includes/fieldset.html template, a lot of manipulations happen to that string. Essentially no matter what you do, the string gets forced back into unicode and (ultimately) escaped in label_tag. Trying to use mark_safe or SafeUnicode or even the |safe template filters all fail to prevent the escaping that takes place.

What that means is that you have three options:

  1. Do a lot of hacking in the django form internals to carry a SafeUnicode string all the way through unharmed.

  2. Manually construct the field's label tag in the admin/includes/fieldset.html template. Beware that there are a lot of important attributes that go on that label like id, for, class, etc.

  3. Create a template filter that parses the string inside the label tag and converts it into a link for you.

Option three there may actually be the simplest if you're any good at all with regexes.


This is something that should really be handled in the template.

Here's one way to go about it...

You can make a whole other model called "Description", then make "Item Type" as an entry in that 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, look into http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#overriding-admin-templates for modifying admin templates. Where you are editing the class A objects, you can insert an anchor to the permalink of that Description object.

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

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号