Chase those goals!!

Chasing your goals is a way to help people live a healthier life. Continuously chasing your goals will keep you looking for new ways to better yourself. Also, you will be less motivated to do things…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Finite State Machines With Django

How to define finite state machines for models using Django

In software engineering, we often need to design a model of our application’s behavior. Finite state machines represent a design pattern that is widely used for this topic.

Before jumping into any example or piece of code, let’s explain what does a finite state machine mean.

A finite state machine is simply a model of computation to simulate a sequential logic. The model which we define the finite state machines for, can be in exactly one of the finite number of states at a time.

The state (or status) can change from one to another in response to some external inputs. This process is also known as a transition.

This is the simplest example to use and understand because it has three states.

The default (or the first) state is the green light. The state changes allowed are as followed:

In Django, these finite state machines and transitions can be defined by specifying the source and the target of the transition. Let’s dig into code!

The library is called “django-fsm” and it’s very simple to use. First, make sure to install it:

The state field has to be an FSM field with a default value. Therefore; the FSMField is imported. In our case, we have the green light as the default one:

The attribute protected=True prevents changing the state directly, as shown in the example below:

Additionally; the transitions need to be defined using the @transition as a decorator and specifying the FSM field we added above. Other variables that the decorator takes are for example the source and the target.

The source is the state from which the transition is allowed to happen and the target is the state to which the transition will happen:

When these transitions are defined as shown in the example, it means that they are the only transitions allowed. Any other behavior is not allowed. For example, we don’t have a transition specified as “greenred”. In this case an exception will be thrown.

So far, our model looks something like this:

Furthermore; let’s take a more advanced example with a ticketing system. There are tickets with different state machines and transitions defined like in the graph below:

The ticket can be created, picked up, marked as failed or closed by a particular user.

The source parameter in the @transition decorator accepts an array to specify more than one state. This is how it looks code-wise:

Based on the transitions, this is how the model currently looks:

Now that the basic concept is clear regarding Django FSM, we want to add some logging for these transitions. This helps to add some description and track by whom the transition was made.

There is another library supported for logs which has to be installed first:

And then, also added in the Django apps:

The parameters can be updated in the method when calling the transition:

In the end, the model for ticket will look like shown in the code below:

We either want to see the logs by using ORM (1) or directly in the Django admin panel (2). Fortunately; they are both possible!

Implementing logs to see them in Django admin (2):

A finite state machine is a model of computation that simulates a sequential logic. Changing from one state to another is known as a transition.

These transitions can be defined for a model in Django. The Django FSM libraries are used for implementing the states, transitions and the logging. More than one source state can be specified for the same target state.

Logs can be implemented to use with ORM or also view them in a cool table in the Django admin.

Add a comment

Related posts:

The Truth Behind Some of the Most Commonly Used Stock Photos

You know those images I’m talking about. You see them at least twice a day on blog posts by writers who download those free photos from Unsplash, Pixabay, and other similar sites. We have come to…

My notes on our approach for project

The Project Literature survey outline is about what technologies and methods we will use in our project (Crime Analysis in Chicago) to achieve our defined goal which is to provide resourceful…

Why agriculture trade shows are essential for farmers and ranchers

Agriculture trade shows are vital events for farmers and ranchers alike. These gatherings provide a platform to showcase new farming technologies, products, and services, as well as an avenue to…