1

I'm developing a Django application that uses mongodb in a part of it. My database schema is changing (some fields are deleted/added continuously), so how to integrate it with Django in a way that accepts the changes without changing the old data, and without affecting the search queries?

I have searched about the available libraries, and I found the below:

  • mongoengine and django-mongodb-engine: mongoengine is not supported for a while now and not updated, also django-mongodb-engine required a forked old django-nonrel package. This matches with the question: Use pymongo in django directly
  • djongo: Initially it is working fine with the most updated versions of python and Django, and it accepts the changes in my database models without migrations and Django admin panel is working fine. Later on, after applying some changes, I have faced an issue when it comes to querying the database or listing the data in the admin panel. The old data fails to fit with the new model if the change includes deleted fields.
  • pymongo: The disadvantage is that I cannot use django models or panel and I have to build my own database abstract layer, but the advantage is about the higher control that I will have over the database. It will be like the first solution in Use pymongo in django directly , then I can build some layers for the different database structures that I will have

Using djongo

Let's say that I have a model called Test:

models.py

from djongo import models

class Test(models.Model):
    x = models.CharField(max_length=100)
    y = models.CharField(max_length=100)

I have created a new object as below:

{
  _id: ObjectId("..."),
  x: "x1",
  y: "y1"
}

Then, I have removed the y field and added a new field called z, then I have created a new object, so it is created as below:

{
  _id: ObjectId("..."),
  x: "x2",
  z: "z2"
}

Now, I want to extract all the collection data as below:

python manage.py shell
>>Test.objects.all()
Error as field "y" is not exist in the model
>>Test.objects.filter(z="z2")
Error as field "y" is not exist in the model

I can understand that it cannot be mapped to the new changed model, but I want the old fields to be ignored at least without errors exactly like the queries in mongodb directly.

According to my request, it is the wrong approach to use djongo? Or is there any workaround for to handle that issue? If no, how to apply that properly using pymongo? I expect to change my collection fields with addition or deletion anytime, and extracting all the data anytime without errors.

Yasser Mohsen
  • 1,411
  • 1
  • 12
  • 29

0 Answers0