<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Veli Tasalı</title>
    <description>The latest articles on DEV Community by Veli Tasalı (@tasali).</description>
    <link>https://dev.to/tasali</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F90284%2F9b713930-ea7b-4c94-bb6f-faea406e740b.png</url>
      <title>DEV Community: Veli Tasalı</title>
      <link>https://dev.to/tasali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tasali"/>
    <language>en</language>
    <item>
      <title>Meet Model Object Mapper, a Database Serialization Utility for Django!</title>
      <dc:creator>Veli Tasalı</dc:creator>
      <pubDate>Fri, 04 Jun 2021 13:27:30 +0000</pubDate>
      <link>https://dev.to/tasali/meet-model-object-mapper-a-database-serialization-utility-for-django-1009</link>
      <guid>https://dev.to/tasali/meet-model-object-mapper-a-database-serialization-utility-for-django-1009</guid>
      <description>&lt;p&gt;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 &lt;em&gt;MIT License&lt;/em&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;This section explains some of the most useful features of MOM. You can find the extensive documentation and quickstart guide here: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://django-mom.readthedocs.io/en/latest/usage/installation.html#"&gt;Installing MOM to your Django app&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://django-mom.readthedocs.io/"&gt;Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/velitasali/django-mom/"&gt;Source Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/django-mom/"&gt;PyPI Package&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Modeling After Models
&lt;/h3&gt;

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

&lt;p&gt;To illustrate the point, let's assume that we have the following model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# File: home/models.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;slug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SlugField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primary_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DateTimeField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in the &lt;em&gt;Main MOM File&lt;/em&gt;, you can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# File: mom_data/mom.yaml&lt;/span&gt;

&lt;span class="na"&gt;mom&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;post&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;home.models.Post&lt;/span&gt;
            &lt;span class="na"&gt;lookupField&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;slug&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h3&gt;
  
  
  Inside the Objects
&lt;/h3&gt;

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

&lt;p&gt;Now, let's assume that we are creating the &lt;code&gt;post.slug-field-value.mom.yaml&lt;/code&gt; file that we have talked about:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# File: mom_data/post.slug-field-value.mom.yaml&lt;/span&gt;

&lt;span class="na"&gt;field&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;My Awesome Post&lt;/span&gt;
    &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;2021-02-23 10:25:00+3&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;This is an awesome post.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of the &lt;code&gt;slug&lt;/code&gt; field is &lt;code&gt;slug-field-value&lt;/code&gt; since it is coming from the file name. &lt;/p&gt;

&lt;p&gt;Now you can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./manage.py mom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a result, MOM will find this file and use the &lt;code&gt;slug&lt;/code&gt; 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. &lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;content&lt;/code&gt; field, you can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# File: mom_data/post.slug-field-value.mom.yaml&lt;/span&gt;

&lt;span class="na"&gt;field&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# ...    &lt;/span&gt;
    &lt;span class="na"&gt;content file&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;content.md&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and inside the &lt;code&gt;content.md&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="sx"&gt;#&lt;/span&gt; File: mom_data/content.md

This is an awesome post.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, you can work with different file formats outside of YAML files.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;MOM can improve your workflow with the flexibility it provides and can be helpful in many different ways. &lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>serialization</category>
    </item>
  </channel>
</rss>
