Codementor Events

Working With Structured data with MongoDB in Django - Part 1

Published Jun 27, 2018

A major issue with Django is that it doesn't support MongoDB in it's default database engines. There are times, when we would love to work with MongoDB and Django framework together and at the same time use model based (schema based) database approach. To support this type of situation there are a very few builtin packages and more than often we have to code it manually. Yes, PyMongo is there, but it does not support model creation or schema based data structure for our data and it's hard to validate data with all the constraints. A workaround? Yes, PyMODM.

PyMODM - Python Package for structuring data in MongoDB

PyMODM is built as a wrapper on top on PyMongo, the official MongoDB driver for python. We can use this to build models, schema and all other structures and properties of the data ingested into MongoDB.

Pre Requisites -

  • Basic Python knowledge

  • Basic Object Oriented Programming knowledge

  • Basic MongoDB knowledge (Not necessary)
    Example code shown in this blog will be of Python 3.5 on linux based machines. Changes for other versions would only be in basic python and command line syntax. We will be covering the following topics in this article -

  • Installation

  • Connecting to MongoDB

  • Defining Models

Lets get started!

Installation

pymodm can be installed like any other python package using pip (or conda, if you are using anaconda) as

pip install pymodm

After this, you would of-course need a working MongoDB instance. You can download and setup your own server locally or use any cloud/online hosted mongo database. You can download it for free from www.mongodb.com

Connecting to MongoDB

Now that we have pymodm installed and MongoDB already set up, you might want to start mongodb service if not started before, by using command mongod.

Now, to connect to your local MongoDB instance we use the connect() function of pymodm.

So, lets just see what we actually did here. First we imported the connect() method from the connection module. Then we established a MongoDB connection using a MongoDB Connection String. This string always starts with _ mongodb:// _ and then the URI of the mongodb server. You must always provide the database name in connect(), "TestApp" above, in order to tell pymodm which database to use. This is the database where all of your data will reside within your pymodm application by default.

Defining Models

Firstly, lets talk about what a model is. A model is basically a schema defining all the properties, attributes, design, etc of our collection to represent our data in a convenient object oriented way. (A collection may be thought as a table in respect to SQL but not necessarily).

Now as we are ready, we will begin to define our model classes. MongoModel is the base class for all top-level models (top-level schema design of a collection).

We have 2 types of models in pymodm.

Basic model

This is a basic model, inheriting MongoModel class, where we define all our properties (columns) and their types and attributes. For ex -

Our model, User will have three properties (or columns) - first_name, last_name and email. We mention the type of first_name by fields.CharField() mentioning that it will be a string value and an optional parameter max_length as 50. The field email will be of type Email. The main types of fields which you can have are -

  • CharField()
  • EmailField()
  • BooleanField()
  • DateTimeField()
  • ObjectIdField()
  • IntegerField()
  • ListField()
  • all other check out here.

Models that Reference other Models

Sometimes our models will need to reference other models. There are two ways to do it.

  • Referencing another document of another collection using ReferenceField() type.
  • Referencing documents of another collection using EmbeddedDocumentField() type.

The difference between a ReferenceField() and EmbeddedDocumentField() is that the reference field is like a foreign key to another document in another collection, where both the documents are a separate entity. The embedded document tells us that the embedded document is a part of this document which does not and cannot live on its own.
As inheritance properties differ (is-a and has-a) these differ in a same way. A Student is-a User, as well as a Employee is-a User but Employee has-a Address.

You can either have a single EmbeddedDocumentField of type Address or a EmbeddedDocumentListField to have an array of addresses.

Now as we have designed some models and schema we can now create and query data. The next article will be based on CRUD operations and QuerySet.

Comment and let me know your views.!

Discover and read more posts from Shrey Batra
get started
post commentsBe the first to share your opinion
Show more replies