| | 266 | === Refactored inner Admin ```js``` option to media definitions === |
| | 267 | |
| | 268 | In [5926] a new method of dealing with media definitions was added. It is now |
| | 269 | much more flexible and allows media on more than just a {{{ModelAdmin}}} |
| | 270 | classes. |
| | 271 | |
| | 272 | An example: |
| | 273 | {{{ |
| | 274 | #!python |
| | 275 | |
| | 276 | # OLD: |
| | 277 | class MyModel(models.Model): |
| | 278 | # not relavent, but here for show |
| | 279 | field1 = models.CharField(max_length=100) |
| | 280 | |
| | 281 | class Admin: |
| | 282 | js = ( |
| | 283 | "/static/my_code.js", |
| | 284 | ) |
| | 285 | |
| | 286 | # NEW: |
| | 287 | # in admin.py |
| | 288 | |
| | 289 | class MyModelAdmin(admin.ModelAdmin): |
| | 290 | class Media: |
| | 291 | js = ( |
| | 292 | "/static/my_code.js", |
| | 293 | ) |
| | 294 | }}} |
| | 295 | |
| | 296 | One very subtle thing to note is previously with trunk the documentation stated: |
| | 297 | |
| | 298 | If you use relative URLs — URLs that don’t start with {{{http://}}} or {{{/}}} — then the admin site will automatically prefix these links with {{{settings.ADMIN_MEDIA_PREFIX}}}. |
| | 299 | |
| | 300 | Which is still partially true with newforms-admin, but now when using relative URLs {{{settings.MEDIA_URL}}} is prepended and '''not''' {{{settings.ADMIN_MEDIA_PREFIX}}}. If you are still looking for the old behavior you can accomplish it by doing the following: |
| | 301 | |
| | 302 | {{{ |
| | 303 | #!python |
| | 304 | |
| | 305 | from django.conf import settings |
| | 306 | |
| | 307 | class MyModelAdmin(admin.ModelAdmin): |
| | 308 | class Media: |
| | 309 | js = ( |
| | 310 | settings.ADMIN_MEDIA_PREFIX + "some_file.js", |
| | 311 | ) |
| | 312 | }}} |
| | 313 | |
| | 314 | Make sure the value of {{{settings.ADMIN_MEDIA_PREFIX}}} is a proper absolute URL otherwise it will be treated the same as a relative URL. |
| | 315 | |