Changes between Version 1 and Version 2 of NewformsHOWTO


Ignore:
Timestamp:
01/17/08 10:24:33 (7 years ago)
Author:
lnielsen@…
Comment:

Added another method for adding a column in the change list view.

Legend:

Unmodified
Added
Removed
Modified
  • NewformsHOWTO

    v1 v2  
    189189}}}
    190190
     191'''Another Way'''
     192
     193Add a method to your model and the insert the method name in the {{{list_display}}}.
     194
     195{{{
     196from django.utils.safestring import mark_safe
     197
     198class MyModel(models.Model):
     199        # ...field definitions...
     200       
     201        def my_column(self):
     202                        return mark_safe(u'<a href="">link</a>')
     203        my_column.allow_tags = True
     204       
     205class MyModelOptions(admin.ModelAdmin):
     206        list_display = ( 'my_column' )
     207}}}
     208
     209Note that by default the content returned from {{{my_column}}} will be escaped. To avoid this, {{{allow_tags}}} must
     210be set to true and the string should be marked as safe. Here is two other options that might be handy:
     211
     212{{{
     213# Change header titleof the column:
     214my_column.short_description = 'My nice column'
     215# Make column show the icon for true/false
     216my_column.boolean = True
     217}}}
     218
    191219== Q: I want to add some field specific template content in my change form. How, how? ==
    192220A: Copy the 'change_form.html' file from the admin app. The fields are looped over in the following bit of code:
Back to Top