Django admin: including some data from the database in a change_form
I'm using Django 1.2.4. I have an admin class for a model that has JavaScript media. I would like to make some data from the databas开发者_如何转开发e available to these scripts. I'm not sure what the best way to do this would be.
Perhaps something like this: (not that I think this is valid, just trying to get my point across.)
class FooAdmin(admin.ModelAdmin):
class Media:
js = ("js/baz.js")
raw_js = "var boo = %s; var bar = %s" % (boo, bar)
I considered making a template at /templates/admin/foo-app/foo/change_form.html
, but I'm not sure how I would put the boo
and bar
values into the template. (They are calculated from the database.)
What is the correct approach here?
You can override ModelAdmin.change_view
to pass in whatever variables you wish to the change_form
template.
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view
You could also potentially (untested) define a view in your ModelAdmin
, map it to a URL with ModelAdmin.get_urls
, and pass in that url to the admin media.
That way you wouldn't have to override a template.
精彩评论