I am stuck on this stage where I need to save my model to a particular table. I have installed postgres and now I want to firstly create 2 tables A and B and append instance of model 'modelA' to table 'A' and of 'modelB' to B. I cannot find much on this on googling and stackoverflow in a clear form.
Try running your migrations, so that the tables will be created inside of your DB. This happens in your models.py file. Any name you give to the class of the table in the models.py, it will correspond to the same name, in lowercase and underscores, the table in your PostgreSQL DB, except if you want to override it.
Here are some nice and helpful tutorials, about Django with PostgreSQL:
Creating a Django Web Application with a PostgreSQL Database on Windows
Step-by-Step Python and Postgres Tutorial with psycopg2
Generic videos about Django database and migrations:
* Python Django Tutorial: Full-Featured Web App Part 5 - Database and Migrations
* The Basics of Django Models
I hope this helps. Please let me know if I helped you!
Related
I am working on a django app. I understood django creates some tables for users, permissions, groups in database. In that database, already another django app is running. so, already it has default tables. Now, I need to create other app in same database. But I think it creates conflict on users,groups,permissions. So, I want to create all tables with some other names.
Updated
I looked at django-table-prefix which doesn't work with 1.8+ .I am using django 1.10
My question is
How to create/migrate django app with different table names for users,groups, permissions or is there any other way?
Sorry for my silly question. Thanks
If you have an existing Django project, which creates the default tables, you can just add a new **app* to the project. The app can be dependent upon those tables or not, depending on the functionality of the app. Here's the section on Django's website that discusses projects versus apps: https://docs.djangoproject.com/en/1.10/intro/tutorial01/#creating-the-polls-app.
Advanced Use: Here's the section on how to write reusable apps. https://docs.djangoproject.com/en/1.10/intro/reusable-apps/
I hope this helps.
I am developing a Flask app that queries on an existing Postgres DB. This DB had been created with Django models. Do i need to reverse engineer so my models would be compatible with SQLAlchemy or will that already be. Is raw queries the way to go? I don't want to create new DBs just query and update some values in the existing ones, that had been created in Postgres using my django models.
Go check out sqlalchemy's AMAZING automap feature: http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html
Used it once in my personal project for similar purpose, it works great.
Let's say I have the following table showing how the columns would be declared for a MySQL table: (I can't think of a very realistic example, so here's something that's so silly. This table is created with the help of Excel)
I want to create a model in Django that is compatible with the MySQL table I'll have with the columns declared this way. However, from looking at the Django documentation, I can't find any model field types that in SQL are the same format as those in the picture except for the primary key field.
I did see before that by default, Django handles a database that uses the SQLite Engine, but I want to see if it's possible to handle a database of MySQL tables.
Is there a way to create Django model field types, like MEDIUMINT, TINYTEXT, and SMALLINT, (in MySQL) that are compatible with tables created through MySQL? It's simply a way for me to use the tables that I created myself, not the tables that Django generates automatically once all the models are defined.
In Django you normally create the models and let the framework generate the tables for you. If you have a legacy database you can use python manage.py inspectdb to generate the models from the database (see the documentation). But if it isn't a legacy database and you've created it just now, you are fighting against the framework and making your life more complicated.
I have experience programming in python, but am new to Django. I recently went through the tutorial and eveything made sense, however I am wondering if I can create views from DB tables that already exists without having to create models. Or, if this isn't possible is there a way to create models to generate an ORM with a DB table that already exists?
Basically I have Aggregate metrics in data warehouse tables that I would like to publish to a web page in real time. Is Django the right tool for this? Thanks.
Use ./manage.py inspectdb to create models from an existing database.
Django: If I added new tables to database, how can I query them?
Do I need to create the relevant models first? Or django creates it by itself?
More specifically, I installed another django app, it created several database tables in database, and now I want to get some specific data from them? What are the correct approaches? Thank you very much!
I suppose another django app has all model files needed to access those tables, you should just try importing those packages and use this app's models.
Django doen't follow convention over configuration philosophy. you have to explicitly create the backing model for the table and in the meta tell it about the table name...