I am using MongoDB as a database for my project and I want to store a list of the dictionary as an array of objects in an object in MongoDB from Django. For Eg. {"id":1,"products":[{...},{...},{...},{...}..]} how do I define my model and how to query this. please help
Have a look at Djongo to start with maybe, it's database connector for MongoDB that uses the default Django ORM.
So you will define your models just like you do with mysql, sqlite, etc. But it enables some extra features.
Related
I have a PostgreSQL database with existing tables. I wish to :
Create a set of Python models (plain classes, SQLAlchemy models or other) based on the existing database
Then manage changes in these models with a migrations tool.
The second part I think is easy to achieve as long as I manage to get my initial schema created. How can this be achieved?
So if someone is willing to use SQLAlchemy I found these two solutions:
Straight with SQLAlchemy reflection and automapping
With sqlacodegen
I need to dynamically create database tables depending on user requirements. so apart from a few predefined databases, all other databases should be created at runtime after taking table characteristics(like no of cols, primary key etc.) from user.
I read a bit of docs, and know about django.db.connection but all examples there are only for adding data to a database, not creating tables. (ref: https://docs.djangoproject.com/en/4.0/topics/db/sql/#executing-custom-sql-directly)
So is there anyway to create tables without models in django, this condition is a must, so if not possible with django, which other framework should I look at?
note: I am not good at writing questions, ask if any other info is needed.
Thanks!
You can use inspectdb to automatically generate the models from the legacy database. You can check about it in here.
Or you can use SQL directly. Although, you will have to process the tables in python. Check it here.
I am already having one mysql database with a lot of data, of which tables and migrations are written in sql. Now I want to use the same mysql database in django so that I can use the data in that database.I am expecting that there will not be need for making the migrations as I am not going to write the models in Django again, also what will be the changes/modification I will have to do. For eg: as in middlewares?. Can anyone please help me in this?
From what I know there is no 100 % automatic way to achieve that.
You can use the following command
python manage.py inspectdb
It will generate a list of unmanaged models that you can export to a model.py file and integrate in your django project.
However it is not magical and there are a lot of edge cases so the generated list of model should be manually inspected before being integrated.
More info here : https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-inspectdb
I need to insert a document into mongodb without using the models, just raw documents without following a model. I'm currently doing the following:
db.analyticsNew.insert(documents_list)
But this throws an error saying
AttributeError: 'MongoEngine' object has no attribute 'analyticsNew'
From what I know, I think it can't find the mentioned collection. But the collection exists in the db. Plus, even if this collection isn't there, it should create one.
Any ideas as to whats wrong and how to fix it?
MongoEngine is a Document-Object Mapper (think ORM, but for document
databases) for working with MongoDB from Python.
That means you have to work with models which are python classes each represents a mongo collection.
If you need to run raw queries you may think to use pymongo.
MongoEngine is built on top of pymongo. It exposes pymongo and therefore you can use it for raw queries as well.
For instance have a look at this.
I'd like to use SpiffWorkflow in conjunction with Django, but apparently SpiffWorkflow can only serialize its states to JSON and XML:
https://github.com/knipknap/SpiffWorkflow/tree/master/SpiffWorkflow/storage
SpiffWorkflow allows serialization of running workflow, so I could store it essentially as a byte stream somewhere (either in filesystem or in Django's DB). But that deprives me of all the advantages of Django.
Is there some way of mapping dictionary or deserialized JSON structure onto objects that Django can use as a regular Django object (stored by Django ORM in a database)? Would writing such a Django backend for SpiffWorkflow/its workflow's JSON representation be complicated? I'm asking because I have basically no experience in Django.
You can use NoSQL as database backend instead of a RDBMS. I suggest MongoDB as it uses JSON notation to store data and could be used in Django projects using mongodb-engine.
MongoDB is schemaless (read MongoDB website article about being schemaless). You may store your data with any structure you want and change it later on the fly.
There are also other NoSQL backends supported by Django like Redis, Elasticsearch and etc. and you can take a look at them to find the best fit for your special needs.