Code

Changes between Version 9 and Version 10 of NewformsHOWTO


Ignore:
Timestamp:
02/18/08 08:12:30 (6 years ago)
Author:
brosner
Comment:

Changed the usage of Options and Inlines in subclassed classes. Options is to scoped and Inlines should be pluralized. Cleaned up things.

Legend:

Unmodified
Added
Removed
Modified
  • NewformsHOWTO

    v9 v10  
    3333 
    3434== Q: How do I change the admin options? == 
    35 A: Options are now separate from the models themselves. You'll have to define a class inheriting from admin.ModelAdmin and define your options in there. 
     35A: Options are now defined in its own class outside of the model. Define your own class that inherits from admin.!ModelAdmin. 
    3636E.g. 
    3737{{{ 
    3838#!python 
    39 class MyModelOptions(admin.ModelAdmin): 
     39class MyModelAdmin(admin.ModelAdmin): 
    4040    save_on_top = True 
    4141    list_display = ('name','approved',) 
     
    5353                ) 
    5454}}} 
    55 To associate the options with the model you pass the ModelAdmin class as the second option to the register function 
    56 {{{ 
    57 #!python 
    58 admin.site.register(MyModel, MyModelOptions) 
    59 }}} 
    60  
    61 == Q: How do I set up edit_inlines? == 
     55To associate your admin class with the model you pass the !ModelAdmin class as the second option to the register function 
     56{{{ 
     57#!python 
     58admin.site.register(MyModel, MyModelAdmin) 
     59}}} 
     60 
     61== Q: How do I set up edit_inline? == 
    6262A: You'll need to do two things. First, set up the options for the models to be edited inline: 
    6363{{{ 
    6464#!python 
    65 class MyOtherModelInlines(admin.StackedInline): 
     65class MyOtherModelInline(admin.StackedInline): 
    6666    model = ReportTemplateXML 
    6767}}} 
     
    6969{{{ 
    7070#!python 
    71 class MyOtherModelInlines(admin.StackedInline): 
     71class MyOtherModelInline(admin.StackedInline): 
    7272    model = MyOtherModel 
    7373    extra = 2 
    7474    template = 'my_new_template_tabular.html' 
    7575}}} 
    76 To use the inlines you need to define the 'inlines' attribute in the parent model: 
    77 {{{ 
    78 #!python 
    79 class MyModelOptions(admin.ModelAdmin): 
     76To use the inline you need to define the ```inlines``` attribute in the parent model: 
     77{{{ 
     78#!python 
     79class MyModelAdmin(admin.ModelAdmin): 
    8080    save_on_top = True 
    8181    search_fields = ['title', 'client__name'] 
    8282    list_display = ('title', 'client', 'approved') 
    8383    list_filter = ('approved',) 
    84     inlines = [MyOtherModelInlines] 
     84    inlines = [MyOtherModelInline] 
    8585}}} 
    8686 
    8787== Q: How do I change the attributes for a widget on a field in my model. == 
    88 A: Override the 'formfield_for_dbfield' in the ModelAdmin/StackedInline/TabularInline class 
    89 {{{ 
    90 #!python 
    91 class MyOtherModelInlines(admin.StackedInline): 
     88A: Override the ```formfield_for_dbfield``` in the !ModelAdmin/StackedInline/TabularInline class 
     89{{{ 
     90#!python 
     91class MyOtherModelInline(admin.StackedInline): 
    9292    model = MyOtherModel 
    9393    extra = 1 
     
    101101 
    102102== Q: How do I change the attributes for a widget on a single field in my model. == 
    103 A: Again, override the 'formfield_for_dbfield' method: 
     103A: Again, override the ```formfield_for_dbfield``` method: 
    104104{{{ 
    105105#!python 
     
    112112 
    113113== Q: I've defined a new permission. How do I enforce it? == 
    114 A: Override 'add_view' and 'change_view' in the ModelAdmin class: 
    115 {{{ 
    116 #!python 
    117 class MyModelOptions(admin.ModelAdmin): 
     114A: Override ```add_view``` and ```change_view``` in the !ModelAdmin class: 
     115{{{ 
     116#!python 
     117class MyModelAdmin(admin.ModelAdmin): 
    118118    save_on_top = True 
    119119 
    120120    def change_view(self, request, obj_id): 
    121121        if request.user.has_perm("my_new_permission"): 
    122             return super(MyModelOptions, self).change_view(request, obj_id) 
     122            return super(MyModelAdmin, self).change_view(request, obj_id) 
    123123        else: 
    124124            raise PermissionDenied 
    125125}}} 
    126 The above will enforce the permission on both GET and POST requests. Put in an 'if request.method =="POST":' or similar if you want to distinguish between the two. 
     126The above will enforce the permission on both GET and POST requests. Put in an ```if request.method =="POST":``` or similar if you want to distinguish between the two. 
    127127 
    128128== Q: I've tweaked something which generated a message for the user but I don't want them to see it. How can I get rid of it? == 
     
    134134 
    135135== Q: How to I add an extra column to the change list view? == 
    136 A: I've got a report model and I've added a link on the change view to allow users to download the report as a Word file. To add your own link you'll need to do two things. First create the following template tags (in 'project/app/templatetags/change_list_extras.py'): 
     136A: I've got a report model and I've added a link on the change view to allow users to download the report as a Word file. To add your own link you'll need to do two things. First create the following template tags (in ```project/app/templatetags/change_list_extras.py```): 
    137137 
    138138{{{ 
     
    181181}}} 
    182182 
    183 Then create a change_list template (in 'project/<YOUR_TEMPLATE_DIR>/admin/<APP>/<MODEL>/change_list.html') 
     183Then create a change_list template (in ```project/<YOUR_TEMPLATE_DIR>/admin/<APP>/<MODEL>/change_list.html```) 
    184184{{{ 
    185185#!python 
     
    191191'''Another Way''' 
    192192 
    193 Add a method to your model and the insert the method name in the {{{list_display}}}.  
     193Add a method to your model and the insert the method name in the ```list_display```. 
    194194 
    195195{{{ 
     
    204204        my_column.allow_tags = True 
    205205         
    206 class MyModelOptions(admin.ModelAdmin): 
     206class MyModelAdmin(admin.ModelAdmin): 
    207207        list_display = ('my_column',) 
    208208}}} 
     
    223223 
    224224== Q: I want to add some field specific template content in my change form. How, how? == 
    225 A: Copy the 'change_form.html' file from the admin app. The fields are looped over in the following bit of code: 
     225A: Copy the ```change_form.html``` file from the admin app. The fields are looped over in the following bit of code: 
    226226{{{ 
    227227#!python 
     
    254254 
    255255== Q: How do I add new object tools to the top right of my change form? == 
    256 A: Create an 'admin/app/model/change_form.html' template and override the 'object-tools' block: 
     256A: Create an ```admin/app/model/change_form.html``` template and override the ```object-tools``` block: 
    257257{{{ 
    258258#!python 
     
    422422N.B. You'll have to learn how to edit ODT and DOCX xml by yourself. Good luck with that. 
    423423 
    424 == Q: Where did prepopulate_from go? My SlugFields don't work! == 
    425 A: There's no longer a 'prepopulate_from' option on database Field classes. Instead, there's a new 'prepopulated_fields' option for your options class. This should be a dictionary mapping field names (as strings) to lists/tuples of field names that prepopulate them 
     424== Q: Where did prepopulate_from go? My !SlugField don't work! == 
     425A: There's no longer a ```prepopulate_from``` option on database Field classes. Instead, there's a new ```prepopulated_fields``` option for your admin class. This should be a dictionary mapping field names (as strings) to lists/tuples of field names that prepopulate them 
    426426 
    427427== Q: What happened to filter_interface? == 
    428428 
    429 A: Use 'filter_vertical' and 'filter_horizontal' on the Options class like so. 
     429A: Use ```filter_vertical``` and ```filter_horizontal``` on the !ModelAdmin class like so. 
    430430E.g. 
    431431{{{ 
    432432#!python 
    433 class MyModelOptions(admin.ModelAdmin): 
     433class MyModelAdmin(admin.ModelAdmin): 
    434434    filter_horizontal = ('some_field',) 
    435435}}} 
     
    441441== Q: How do I add custom validation? == 
    442442 
    443 Use newforms as you would normally. Just look at the `ModelAdmin` class to see the hooks. 
     443Use newforms as you would normally. Just look at the ```ModelAdmin``` class to see the hooks.