Using mixins

Now we are going to use AnyBlok mixins to:

  • Refactor our code in order to use IdColumn mixin to create the id primary key column on the room model
  • Use the TrackModel mixin that adds 2 fields to track creation and last modification date:

Note: You can clone III-03_create-model branch from AnyBlok/anyblok-book-examples repo to get ready to start with this chapter.

Install anyblok_mixins

Add [anyblok_mixins][pypi_anyblok_mixins] to your package requirements and install the package.


 requirements = [
+    'anyblok_mixins',

Declare anyblok-mixins blok as required in the Room blok init file.

file: rooms_booking/room/

  class Room(Blok):
     """Room's Blok class definition
     version = "0.1.0"
     author = "Pierre Verkest"
-    required = ['anyblok-core', 'address']
+    required = ['anyblok-core', 'address', 'anyblok-mixins']

Add test

In the following test we make sure as requested by our customer that edit_date field is properly updated after a change occurs on a room.

#File: rooms_booking/room/tests/

from datetime import datetime
import time
import pytz

class TestRoom:
    """Test Room model"""


    def test_track_modification_date(self, rollback_registry):
        registry = rollback_registry
        before_create =[0]))
        room = registry.Room.insert(
        after_create =[0])) = "A2"
        after_edit =[0]))
        assert before_create <= room.create_date <= after_create
        assert after_create <= room.edit_date <= after_edit

Note: room.flush() is used here to force sending data to the database as edit_date field is configured with auto_update option wich is called before sending data to the database.

Note: room.refresh() is used to clear cache on the ORM side to request data from the database. It will perform a flush before if necessary.

Those methods are rarely used in your code but can be tricky while playing with some kind of fields.

Reuse mixins

Using multiple inheritance capability we inherits from Declarations.Mixin.IdColumn and Declarations.Mixin.TrackModel. Diff result:

 from anyblok import Declarations
 from anyblok.column import String, Integer

 Model = Declarations.Model
+Mixin = Declarations.Mixin
 register = Declarations.register

-class Room:
+class Room(Mixin.IdColumn, Mixin.TrackModel):

-    id = Integer(primary_key=True)
     name = String(label="Room name", nullable=False, index=True)
     capacity = Integer(label="Capacity", nullable=False)

results matching ""

    No results matching ""