I have a huge MongoDB database and I want to generate the Django models dynamically so I don't do it by hand (it would save me a lot of work).
I have used inspectdb before to generate the models, but according to the official Django documentation, it is only available for relational databases ( PostgreSQL, MySQL and SQLite ).
Any alternative to generate models with MongoDB and import them into my models.py?
Thinking about this a little, I have doubts about whether it is possible. Keep in mind that a document
MongoDB
does not have to be "constant" or uniform within the same collection, which makes the automatic generation of the model complicated. For example, you might have a person collection with a document similar to this:And, in the same collection, another document:
Both documents are valid, but a possible code generator would create a different model depending on which of the two documents it analyzed. In order to get a valid model for both documents you would have to parse the entire collection (which would be a problem with large collections). Let's suppose that the generator solves it in a simple way, looping through the entire collection and assuming that languages can be an optional property.
This other document would also be valid (would
MongoDB
allow insertion):Here the generation logic is complicated exponentially. Some documents would have a cardinality=1 property and others would have a property with N values. Of course, this situation would be resolved if the collection were homogeneous, but
MongoDB
it does not guarantee it.These points help explain why you
inspectdb
work with relational databases and notMongoDB
.I understand that this may not be considered a valid answer, although as a comment it is unreadable, and I'll delete it if necessary.