BaseModel Model - DDMAL/CantusDB GitHub Wiki

BaseModel (code) is an abstract model in the main_app app. It inherits from django.db.models.Model.

All models in main_app inherit from BaseModel: Century, Feast, Genre, Indexer, Notation, Office, Provenance, RismSiglum, Segment, and Source inherit from BaseModel directly, while Chant and Sequence inherit from it through BaseChant.

Fields

  • date_created: a DateTime field, representing when the object was initially created
  • date_updated: a DateTime feild, representing when the object was most recently updated
  • created_by: a ForeignKey field, pointing to a User object
  • last_updated_by: a ForeignKey field, pointing to a User object

Methods

  • save(): Overwrites/extends django.db.models.Model's save() method, running .full_clean() on the object before saving it.
  • display_name (property): returns an object's string representation. On pages that can only be accessed by logged-in users, dispay_name can be used when rendering objects in templates (though usually, an object's name, title, incipit, etc. is a better choice - this way the field can be fetched directly from the database, rather than being calculated on the fly. Also, there are not many situations where we want to display an object's ID on a template, and IDs are included in most/all objects' string representations).
  • get_absolute_url(): returns the URL of the object's detail page. This method will return a string for every object, but the string will only be a working url if the object's model has an associated detail view (see Views).
    • In the past, get_absolute_url was used in templates in order to link to objects' detail pages, but there are better ways to do this - when tempted to use {{ chant.get_absolute_url }} in a template to link to a Chant Detail View, for example, use {% url 'chant-detail' chant.id %} instead. The get_absolute_url method is retained for ease of testing, and should not be used in templates unless there is no other option. One exception is the Content Overview page, where objects of any class may appear in the list of recently updated objects.
  • get_verbose_name(): a classmethod. Calling an object's .get_verbose_name() will return the object's model's verbose name.
  • get_verbose_name_plural(): a classmethod. Calling an object's .get_verbose_name_plural() will return the object's model's plural verbose name.
  • get_fields_and_properties(): a classmethod. Calling an object's .get_fields_and_properties() will return a list of all its properties and methods.