how to sync django models with pre-existing database? - django

I am having a hard time trying to come up with a reasonable design for my project. I have an existing Postgres database that gets constantly updated from other Python scripts.
The web server built on Django framework will access the Postgres database to update User models only and display blog information for the logged in Users. The blog information is what is being updated overnight by other Python scripts.
Now my question, if I have to syncdb my blog model with existing Postgres database, would that cause any problem?
ex:
models.py
class Blog:
title=...
content=...
author=....
And say my Postgres db called mydb has many tables, one of which is blog table and contains columns for title, content and author.
How would make my model in sync with existing database?
Now lets say I included a new column in my db which is date of entry.
If I simply update my model to :
class Blog:
title=...
content=...
author=....
date of entry=...
will it work.
what are the potential problems here and any simpler solutions for these?
P.S: I have used South in the past. but the situation here is different. I am using db that is read-only from Django's point of view, and no data migration is necessary as of now.

If your database is read-only, you don't have to do syncdb. Use managed=False and the db_table meta option on your model to specify the table name it corresponds to, and likewise for the field column names.
If you haven't already, see the doc on legacy databases for more info.

Related

Detect database DDL schema changes with Django

Let's say that we have a Django app that looks on a legacy database.
If someone make changes on some database tables from a db client as DBeaver for example and not through Django models, is there a way to identify these changes?
You can do in a terminal, inside your Django project directory : python manage.py inspectdb > models.py
You will have models related to your tables.
By default, inspectdb creates unmanaged models. That is, managed = False in the model’s Meta class tells Django not to manage each table’s creation, modification, and deletion.
If you do want to allow Django to manage the table’s lifecycle, you’ll need to change the managed option above to True (or remove it because True is its default value).

is there any way can i use the Django for existing database? if yes how to use models?

I am working on already existing data on relational database. Now question is that how to build models and how to update the tables with new data coming from user (technically django forms)?
Django natively supports creating models for and working with existing data. From the documentation:
Integrating Django with a legacy database
Django will still need to create several of its own tables, but will adapt to use your existing tables. From the doc, you can auto-create models like this:
python manage.py inspectdb > models.py
You'll need to determine whether you want to manage updates to the table structure, but that's getting into details that will be specific to your project.

Do i need models.py even for ready made mysql databases?

I spin up a django project. Afterwards, i didn't write models.py but instead I created a database from MySQL command line(independent from django) and created three tables with required columns. Finally i connected my django app with that database successfully. I applied migrations. But now i am confused do i need to write models.py with every field name as in column?
I remember implementing a basic project in which i did write models.py and created database using "python manage.py shell" and then put values using
"from polls.models import Choice, Question"? How do i put data now initially and then using python on some action from UI?
Do i need models.py even for ready made mysql databases?
You do not need to construct models. Some (small) webservers are even completely stateless, and thus do not use a database. But a large part of how Django can help you is based on models.
You can write your own queries, forms, etc. But often by using a ModelForm, Django can for example remove a large amount of boilerplate code. It will make it furthermore less likely that there are mistakes in your code. So although not strictly necessary, the models are usually a keystone in how Django can help you.
You can use the inspectdb [Django-doc] command to inspect the database, and let Django "sketch" the models for you. Usually you will have still some work. Since Django can, for example, not derive that a field is an EmailField, since both a CharField and EmailField look exactly the same at the database side.
You do not need to use inspectdb however. You can construct your own models. If you create your own models, but these exist already at the database side, you might want to set managed = False [Django-doc] in the Meta of your model, to prevent Django from constructing migrations.

Database table for bus booking website

I'm creating a bus booking website using Django. I'm stuck at what the database tables should look like. So far I've created the displayed tables, I'm very poor with databases so please mention foreign keys and primary keys when answering. Also, I'm stuck on how to actually book seats in Django, like what will be the code in the background and what other packages should I use. Thanks.
Table- Route:
Columns - route_id (primary key),
location_from,
location_to,
route_name,
Table - Bus:
Columns - Bus_id(primary key), type_of_bus,
bus_registration,
capacity,
bus_number,
route (foreign key)
Also I'm using the default sqlite database that comes with django. Is it good enough to build this kind of website or do I need to change? This website is just a project and will never go into production phase.
I'd highly recommend writing your database schema in Python classes (as Django models) and then using ./manage.py makemigrations ./manage.py migrate, which will look at your model code and create the corresponding database schema. This portion of the django tutorial could be helpful to you.
The python code corresponding to your example for a route would look something like:
from django.db import models
class Route(models.model):
location_from=models.CharField()
location_to=models.CharField()
route_name=models.CharField()
This is a much easier way to write a database schema, especially if you're not so hot with databases.
If your app will never be in production, SQLite is fine - it's very similar to other database engines for toy projects.
As for what your data model should look like, I think it depends how complex you want to make the app. For a start, I think you could add a Location model, which your Route model should reference. For making reservations, you could look into django booking - I haven't used it but it comes up on django packages when you search 'booking'.

Connecting and importing models from multiple database connections Django

In a django app, I have two postgresql databases connected through settings.py: one is default and other is AppDb. AppDb is placed on a remote machine.
I want to query from a 'Courses' model from AppDb using 'using()' and 'Courses' model is not available in default database.
So my query goes like this:
courseInfo = Courses.objects.using('AppDb').filter(cuser_id = 12)
But I am getting NameError for 'Courses'
Can I have a solution for such queries without using routers
If you have an existing database, you still need to create an app and models for that database in order to use the ORM.
To help you create the model classes, you can use the inspectdb management command which will try to guess the models from an existing database and create the models.py for you. Its not perfect, but it will save you some time.
You will still have to make sure the models have a primary key and are written in the correct order (so that foreign keys will work correctly).