Back
Feb 18, 2010

Business logic in models

In my recent project there was a lot of data business logic, so I had to organize this code somehow. In this article I'll describe a few hints on how to it.

Mixins

Simple business logic functions should in models or managers. When there's a lot of them, the can be split in mixins.

# logic.py
class EventLogic(object):
    def start(self):
        if self.condition():
            # ...


# models.py
class Event(models.Model, EventLogic):
    name = models.CharField()
    # ...

Custom exceptions

For business logic errors you should declare custom exception classes. It's not fun to understand what this specific IntegrityError means.

Generic views

When all logic is in model and can throw, say, BusinessLogicException, we can write views like this:

from django.contrib import messages


def event_action(requets, event_id, action):
    # event = get_object_or_404(...)
    try:
        getattr(event, action)(request.user)
    except BusinessLogicException, e:
        messages.warning(request, e.message)

    return redirect(request.POST.get('next') or event)

Subscribe for the news and updates

More thoughts
Feb 12, 2020Technology
5 Best Payment Gateways For 2020

We reviewed the best payment gateways in 2020. Here’s our comparison of their features, advantages, and disadvantages.

May 9, 2018Technology
How to Generate PDF Files in Python with Xhtml2pdf, WeasyPrint or Unoconv

Programmatic generation of PDF files is a frequent task when developing applications that can export reports, bills, or questionnaires. In this article, we will consider three common tools for creating PDFs, including their installation and converting principles.

Aug 8, 2016TechnologyBusiness
How To Add HTML5 Geolocation To Your Web App?

In this article I will describe how to integrate geolocation HTML5 function to a web app so you can then easily implement it in your apps or websites. As an example we are going to create small web app which will be able to calculate the shortest route between detected user’s location and predefined destination using Google Maps API.

Apr 3, 2011Technology
Sprite cache invalidation

When we use css-sprites it's important to make browser cache them for longest period possible. On other hand, we need to refresh them when they are updated. This is especially visible when all icons are stored in single sprite. When it's outdated - entire site becomes ugly.

Oct 11, 2010Technology
Testing authentication in Django

In order to check if user is authentcated in test, you can run:

Sep 23, 2010Technology
Dynamic class generation, QuerySetManager and use_for_related_fields

It appears that not everyone knows that in python you can create classes dynamically without metaclasses. I'll show an example of how to do it.So we've learned how to use custom QuerySet to chain requests:Article.objects.old().public()Now we need to make it work for related objects:user.articles.old().public()This is done using use_for_related_fields, but it needs a little trick.