Skip to content

Coding Style

James Andrews edited this page Sep 6, 2021 · 5 revisions

General Views

If performing an action via a POST, redirect back to the GET version as per https://en.ryte.com/wiki/Post-Redirect-Get e.g. at the end of the if POST code return HttpResponseRedirect(reverse("classification_import_upload")) Note you can still communicate the results of the POST via messages.

General Forms

If a form has a single field, wrap the input with

<div class="input-group">
    <input class="form-control" />
    <span class="input-group-append">
        <input class="btn" ... etc />
    </span>
</div>

As well as making it obvious that the button applies to that input field, hitting enter in the field will cause the button to fire (thanks to global.js).

General Text

If there's static text on a page that the user could learn, put in in <p class="text-info">Text goes here</p>

Admin Pages

See clinvar_export_admin.py for some good examples Using standard @admin.register(Model) decoration to mark a class as admin instead of admin.site.register

Defining has_add_permission, has_change_permission, has_delete_permission there’s a lot of models where it makes no sense for a user to manually create one, and removing unnecessary permissions tidies up the admin UI a little.

Providing standard filters without needing to create a whole filter class, e.g.

list_filter = (('status', AllValuesChoicesFieldListFilter), ('clinvar_key', admin.RelatedFieldListFilter))

Making all the admin classes extend ModelAdminBasics which is normally pretty good at marking fields you should never edit as readonly, provides a JSON editor. In addition extending this class will allow you to use the decorator

@admin_action("Validate It")
def validate_it(self, request, queryset):
    ....

where the action will:

  • Automatically be added to the list of actions (no need to define actions=["method1","method2"] in the Admin class)
  • Be assigned the short description of "Validate It" or whatever value was passed there.
  • Provide an info message of how many records were selected to have that method called on it.

In addition, if you want to display a non-standard list column, there's a decorator for that too - BUT you will still have to include the name in the list "list_display", this is because the order of these columns is visually important.

See ClassificationAdmin for an example where created_detailed and modified_detailed change how those columns are rendered.

Use of TabularInline to show child objects on the view from a parent object

Overriding get_form to mark some fields as AdminTextInputWidget (stops them from being a. full text area). Note that the model ClinVarExportBatch has

    class Meta:
        verbose_name = "ClinVar export batch"

So the admin screen gives “ClinVar” the right capitalisation

I never noticed before, but if you provide a model with get_absolute_url the view page in admin will have a “View On Site” link at the top right that will take you to the regular webpage for the object.

If you look at ClassificationAdmin you’ll see some of the columns in list_display which aren’t on the model, e.g. created_detailed you can also see the definition of created_detailed where it’s given a short description and an admin_order_field (so you can still order by the column)

Clone this wiki locally