How does dependencies isolation between MongoDB and Django work? - python

How does Isolation dependencies between mongdb and django work?
I have made several projects with JAVA/SPRING, and the recent days,
I am studying Python django .
I successed to get connection between django and sqlite, and few days ago,
when I tried to connect to mongodb, it is hard to seperate dependencies database and django because of django-admin.
Django-admin require to attain specific Fields to each model, however, every database has each their own Field properties. Therefore, when project should change database structure, we should change a lot of code in model.py.
e.g
What if sqlite to Mongodb? only using different driver cannot make server work. e.g models.TextField(sqlite) -> models.StringField(mongodb). It is unavoidable, right? It seems difficult that solating dependencies completely between database and django
Is it Okay? do I miss something?

What if sqlite to Mongodb? only using different driver cannot make server work. e.g models.TextField(sqlite) -> models.StringField(mongodb). It is unavoidable, right? It seems difficult that solating dependencies completely between database and django
TextField and StringField isn't where you will have problems since it is supported in almost all databases, but consider this library django-mongodb-engine, it reuses the basic fields that already exist in Django ORM to the extent of what's possible, e.g.
class Post(models.Model):
title = models.CharField() <-- both Django ORM and mongodb-engine have this
text = models.TextField() <-- both Django ORM and mongodb-engine have this
This model will work as is both in SQL and MongoDB engines, but if you use a MongoDB-only (as in not in default Django ORM engine) feature like ListField()
class Post(models.Model):
title = models.CharField()
text = models.TextField()
tags = ListField() # <-- not supported everywhere
Then you migration to a SQL engine will involve some manual work to map that field.
Therefore, when project should change database structure, we should change a lot of code in models.py. Is it Okay?
Typically yes, to change the database structure you make changes in models.py. After which you would be running a migration, which is a step that generates scripts to alter the database (e.g. adding a new SQL table column). For MongoDB there's no explicit migration, as its schema is flexible (or schemaless if you prefer).
As for the admin, it's fairly smart when it comes to Django models, you may need to define the models and fields you want to appear in the admin, but in the detail view you don't have to worry about every single field, as Django admin understands them based on the information in models.py
And to change database drivers, it's the DATABASES value in settings.py

Related

Django Rebuild all migrations

I am building an app in Django and it uses a live/in-use database.
Basically since the apps development the SQL Database has undergone some structure changes and it is causing issues with Django, Django will try to apply migrations to the database that already exist. For example:
In the Django app I marked the email column as unique which was fine based on the development database. However the main database now always has a table change that marks the email column as unique. Django is fighting this unique key with the one that already exists.
So is it possible to clear all Django migrations and have it make migrations again compared to the more up to date SQL database structure?
If your models are very out of sync with your database, the easiest option is probably to rebuild your models from scratch using inspectdb.
If your models are pretty close to the database already, the first step is to make sure that your models match the database exactly. You can use sqldiff from django-extensions for this.
Once your Django models match your database, follow this answer to re-create migrations based on the existing database schema.

How to change Django's default Postgres database search directory

I am creating a Django web application and I am using Postgres for my database.
Under my project, I have a web application named 'home', and I created a table named 'myTable' in Postgres.
Whenever I try to save something in the table, Django automatically looks for the table called 'home_myTable' instead of 'myTable'. For example, if I do
python manage.py migrate
I get the following error:
django.db.utils.ProgrammingError: relation "home_myTable" does not exist
I have been working around this by manually giving Postgres commands using Psycopg2, but this is really annoying.
Is there a way to stop Django from automatically start looking for 'home_myTable' and instead make it search the table that I want?
You can set the db_table Meta option for you model.
class MyTable(models.Model):
...
class Meta:
db_table = 'mytable'
Unless you are dealing with a legacy database, I recommend that you define your models, create migrations, then let Django create the tables, instead of creating the tables manually as you are doing.

Database first Django models

In ASP.NET there is entity framework or something called "database first," where entities are generated from an existing database. Is there something similar for Django?
I usually work with a pre-existing database that I need to create a backend (and subsequently a front end) for. Some of these relational databases have many tables and relations so manually writing models isn't a good idea. I've scoured Google for solutions but have come up relatively empty handed.
You can use the information on this link.
python manage.py inspectdb > models.py
https://docs.djangoproject.com/en/3.0/howto/legacy-databases/
It depends on your database, but I've worked with it and is good.
The default in Django is a Code First-type approach where the Django framework engine creates a Db for you based on your models and uses migrations to update the Db with model changes (like Code First).
https://docs.djangoproject.com/en/1.11/intro/tutorial02/
What you're describing sounds like a Database First approach in the .Net world.
Integrating Django with a legacy database:
https://docs.djangoproject.com/en/1.11/howto/legacy-databases/

Django model creation from existing MongoDB databases

I have a few databases in MongoDB that I want to create models for dynamically, since there are many databases and I cannot do it manually. Questions:
What should my models.py look like? (Does inspectdb work with mongodb databases or only SQL based dbs?)
Since the database models are created dynamically, how do I code the serializer class to return the dynamic fields?
Thanks in advance.
Django supports an object-relational mapper, that is aimed at traditional relational databases. While there are a number of mongodb packages for Django, none of them support inspectdb to construct your models. Either way, inspectdb is a kludge designed as a one of process to help a one-of migratation away from a legacy system, i.e. you'd build your models.py file once and never run inspectdb again. This is not what you want to do, as you seem to want dynamic models that can be added or altered at runtime.
On the bright side, Django MongoDB Engine has some support for arbitrary embedded models within pre-defined models. But even then they don't seem too supportive of it:
As you can see, generic embedded models add a lot of overhead that bloats up your data records. If you want to use them anyway, here’s how you’d do it...
In summary, try to build your models as best you can to actually match your requirements. If you know nothing about your models ahead of production, then perhaps Django isn't the right solution for you.

Database Tables not defined in DJANGO models

Is it fine not to define table stucture in models.py...I mean we are using
cursor.execute('some query')
so its okay not to make something like this :
def tblNew(models.Model)
col1 = models.CharField(max_length=2)
Thanks for guidance, Im a newbie in django.
You CAN create database tables directly in postgresql But you'd have to create models.py files later because otherwise you will be missing out on several things. If you have pre-existing database, then you have django-admin.py command to pick them up for you (https://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb)
ORM. It makes alot of things easyer. Writing sql in views? Why would that be good? Because that is what you get if you do not use ORM at all. And while i do have sql in some of my views, it only exists there because django lacks some features like language collation specific sorting. But otherwise writing sql in views hampers readability.
You can't use database migrations. South (http://south.aeracode.org/) is one awesome project that most big django projects use. In fact i think it was most used django project in some apps poll. Django 1.7 will provide the migrations too, but you will need to declare models im models.py for that. What you loose if you do not use database migrations - SANITY. I do not exaggerate here. Keeping track of database changes in development and prelive/live servers is nightmare otherwise.
Also is not using django ORM in some places (not declaring models) kind of strange, while you will definately use django models and ORM elsewhere - or will you stop using django authentication, sessions and site logic also? All those use django ORM...

Categories

Resources