django-model-helpers / README.rst

Full commit

Django modelhelpers


This reusable package conatins some helpers to help automate pre_save model fields processing and helpers to create 'generic' views

Model 'Mixins'



Name for this mixin is strange, please suggest alternative

This mixin allows cusomizing model's detail_view and list_view templates and context from model code


  1. Add "modelhelpers" to settings.INSTALLED_APPS.

  2. Then add this mixin when subclassing Django's models.Model to be able customizing template and context generation from model. Example:

    from django.db import models
    from modelhelpers.mixins import ModelConfiguredViews
    class Movie(models.Model, ModelConfiguredViews):
        DETAILS_TEMPLATE = "movies/movie_details.html"
        LIST_TEMPLATE = "movies/movie_list.html"
        title = models.CharField(max_length=100, unique=True)
        slug = models.SlugField(
            _("Name to be used in urls"), max_length=100, unique=True, blank=True)
        # ...
        def details_prepare_context(self, request, *args, **kwargs):
            if self.owner == request.user:
                return {"is_my_film": True}
            return {"is_my_film": False}

After this your model will have get_absolute_url methods both for class and for model instances.

For instance calls:

This url will point to modelhelpers.view.details_view which will use template DETAILS_TEMPLATE and template context will be build from this dict:

{"instance": Movie.objects.get(slug=slug),
 "is_my_film": True_or_False}

For class methods it will use another template and will contain "queryset" in context

You can inspect code of ModelConfiguredViews for other methods and settings. Code of this mixin is small and easy to understand and it contains docstrings but they are not really needed. It can be used for models without slug or with slugfield with custom name. You can even override methods *_get_template to get templates based on request.


I know that this is not MVC-friendly (in Django terms it should be named Model-Template-View, MTV <=> MVC) but it's sometimes usefull to have model-related code in model. You can assume that Controller is "separately" placed in model's method.


This mixin allows processing fields before save() just by defining ModelClass.process_FOO methods.

This method receives these arguments:
  • old_value — value of field after init.
  • new_value — value of field before save.
  • changed — old_value != new_value.
  • old_values — dict that contains other post_init values of fields. Only stores values for fields that are processed (when model have process_FOO method). All field values can be stored if you set ModelClass.REMEMBER_ALL to True


class NewsItem(models.Model, DummyUrlMixin, AutoProcessFieldsMixin):
    WORD_DELIMITERS = (u" ", u"," u".")

    creation_time = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(
        _("Name to be used in urls"), max_length=100, unique=True, blank=True,
        help_text=_("Auto populated"))

    full_text = models.TextField()
    short_text = models.CharField(max_length=100, blank=True, help_text=_("Auto populated"))