DEV Community

Cover image for Meet Model Object Mapper, a Database Serialization Utility for Django!
Veli Tasalı
Veli Tasalı

Posted on • Updated on

Meet Model Object Mapper, a Database Serialization Utility for Django!

Model Object Mapper, or MOM for short, is a Django Management Utility for statically creating and updating database entries. It supports relational fields and can work with Django apps without requiring any modification. It is free and open-source, and licensed under MIT License.

Introduction

MOM improves your workflow when dealing with personal projects or private datasets that don't require end-user-friendly forms to insert or update. It offers an easy-to-use serialization mechanism that syncs your database every time you run it.

Usage

This section explains some of the most useful features of MOM. You can find the extensive documentation and quickstart guide here:

Modeling After Models

To have MOM recognize our Django models, we first describe how we will use them in a file called Main MOM File. In that file, we tell MOM which files point to which models and how we will handle relational fields.

To illustrate the point, let's assume that we have the following model:

# File: home/models.py

from django.db import models

class Post(models.Model):
    slug = models.SlugField(primary_key=True, )
    title = models.CharField(max_length=100, )
    date = models.DateTimeField()
    content = models.TextField()
Enter fullscreen mode Exit fullscreen mode

Now, in the Main MOM File, you can do:

# File: mom_data/mom.yaml

mom:
    map:
        post:
            model: home.models.Post
            lookupField: slug
Enter fullscreen mode Exit fullscreen mode

Here the map key inside the mom key defines the post key that tells MOM how to handle a home.models.Post model. Following that, MOM finds the files whose names start with post, and continue with the value it will assign to the slug field of home.models.Post, and finally end with the MOM file format. When we bring all the three together, we get something like this: post.slug-field-value.mom.yaml.

Inside the Objects

Since we have created a Main MOM File, we can now focus on objects (or serialization files). In these files, we represent database rows, creating and updating them as they change.

Now, let's assume that we are creating the post.slug-field-value.mom.yaml file that we have talked about:

# File: mom_data/post.slug-field-value.mom.yaml

field:
    title: My Awesome Post
    date: 2021-02-23 10:25:00+3
    content: This is an awesome post.
Enter fullscreen mode Exit fullscreen mode

The value of the slug field is slug-field-value since it is coming from the file name.

Now you can run:

$ ./manage.py mom
Enter fullscreen mode Exit fullscreen mode

As a result, MOM will find this file and use the slug field and its value to query for an existing row. If there is one, MOM will compare its fields for changes. If there isn't one, MOM will create a new one on the database.

One useful feature you can use here is the options enabled by adding a space after the field name. For instance, if you are working with a markdown file and want to use it as the value for the content field, you can do this:

# File: mom_data/post.slug-field-value.mom.yaml

field:
    # ...    
    content file: content.md
Enter fullscreen mode Exit fullscreen mode

and inside the content.md file:

[comment]: # File: mom_data/content.md

This is an awesome post.
Enter fullscreen mode Exit fullscreen mode

This way, you can work with different file formats outside of YAML files.

MOM has many features like this that you can use right now and are explained in the documentation thoroughly. Relational fields may be a good reason to check it out.

Conclusion

MOM can improve your workflow with the flexibility it provides and can be helpful in many different ways.

Top comments (0)