Django: inspectdb doesn't generate all of my tables into models? - python

I have a file - "codemets.db", which contains a lot of tables and data for my project. Some of the tables are:
- auth_group
- auth_group_permissions
- auth_user
- auth_user_groups
- ...
- mappingapp_coordinates
- ...
I am using PyCharm to create a new app on Django. I am trying to use the already existing database "codemets.db" and I am trying to auto-generate the tables into models. I am using the following commands:
python manage.py syncdb
python manage.py inspectdb > models.py
python manage.py makemigrations
python manage.py migrate
However, I am getting all of the models for the tables only until the mappingapp_coordinates.
For mappingapp_coordinates and the others after that, there are no models. Can someone help me with that? How can I generate all of my tables into models?
My second problem is:
when I open the app on the admin page I can see the tables (only the tables that have been generated into models), but there is no content from the database in them (they are empty). How can I put the data from my database into my app?
Thank you in advance.

Related

Switch to another database in Django

I have a django project that was created on an Oracle database and I want to switch to ANOTHER Oracle database. I have followed this tutorial https://pythonfusion.com/switch-database-django/, but there is a problem that not all models are created initially in Django, some are created using inspectdb on existing tables in other databases . Therefore, when using the migrate --database=new command, I get errors about those tables that already existed before Django was created. Is there a way to migrate only the models and tables necessary for Django to work? (users, auth...)
I think you have to take a look at the managed attribute of each model meta class.
If managed is true then django will change the model in the database.
Unmanaged model :
class MyModel(models.Model):
...
class Meta:
managed = False # This means django will ignore MyModel when migrating
Managed model :
class MyManagedModel(models.Model):
...
class Meta:
managed = True # This means django will migrate MyManagedModel
More documentation here : https://docs.djangoproject.com/en/4.1/ref/models/options/
Yes you can definitely customize your migration behavior, the command python manage.py makemigrations creates a couple of files that are used to migrate your models into your DB, any who you can still access these files and choose exactly what to include, exclude and even edit them.
Check the following link:
https://dev.to/koladev/writing-custom-migrations-in-django-3eli
If I've understood your question correctly, then you're looking to use Django's built-in migrations. To find out which migrations have been run against your new database, run the command manage.py showmigrations --database=new which will show you a list of all migrations that exist within the context of your application.
Once that is done, you can manually run the desired migrations (e.g. auth and contenttypes) by running the command manage.py migrate --database=new app_label migration_name.
showmigrations command: https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-showmigrations
migrate command: https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate

How to fetch data from postgres existing tables to Django?

I have a Postgres Database and it has plenty of tables in it, using Django I need to query or get all the table rows into my views.py. After reading I understood that creating a model in models.py will create a new table into my DB which is not what I need.
Is using django a good way to fetch data from postgres or should I use the python library to communicate with postgres directly (psycopg2)?
If django has a way to fetch data from table then how do I do it?
You can inspect the existing database schema by
python manage.py inspectdb
for adding the database model to models.py
python manage.py inspectdb > models.py

"Relation ... does not exist" after deleting few tables

I have a postgres database, which naming midgard_dev. It has a many tables.
After changing in models at product app I got error 'Column product_... does not exist'. I removed few tables in database, which related with product, and got error "Relation products_product does not exist. LINE 1: SELECT COUNT() AS "__count" FROM "products_product".
This error I get, when open page with product (from admin-panel or user-page)
I watched migrations (with ./manage.py showmigrations) and saw, that all migrations successful.
makemigrations app and migrate --fake don't help respectively. Delete the entire database don't want.
The tables that remained associated with the product
Thnx for help.
I had same error, I solved it by typing python manage.py makemigrations model_name then python manage.py migrate model_name to command promt/power shell. Reason could be that typing same commands without model_name do not create tables and/or relations for specific model(not professional opinion).model_name is name of your model.

How to change a sqlite table column value type from Django model?

I created a table before I code the Django app and now I merged both the app and the table with following command python manage.py inspectdb > models.py. However after some while I really need to change the value type of one of the column. Is it enough to chage it through the model file or do I need some additional steps?
If you change a field in a Django model, Django itself doesn't know how to update your database accordingly (syncdb only add tables from new models).
You have two options:
manually create your database tables;
use a migration tool like South that detects and generates migration files from changes made to your models;
I recommend the second option as it's programmatic, more error-proof and makes your life easier when you need to go back and forth between database schemas.
There is an easy way to do this. (in Django 2)
After making the necessary changes to the model.py file of your app, run command:
python manage.py makemigrations - This will generate a new file in migration folder of your app.
python manage.py migrate - This will apply those edits on actual databse.
To check if the changes have been applied, run command : .schema <tablename> in your terminal, after entering the sqlite command-line program.

How to update models.py when I create a new table in my database?

I was wondering how to automatically update my models.py in a specific app in django. I created a new table in my database through HeidySQL, when i syncdb, i see the table appears in the directory in the command line, but not in models.py . I also tried migrate and make migrations but still not working.
Any idea? Thanks!
The manage.py migrate and makemigrations commands work in the opposite direction that you are describing here. If you want to generate a starter template of your models based on the current layout of the database, you can try using the inspectdb command.
python manage.py inspectdb > temp_models.py
This will generate a starter model for every table in the database. You should review and update the generated models so they make sense with your app. Take special note of lines ending with the comment # This field type is a guess.
Once you are satisfied with the new models, copy them over to the app's models.py file.
Hope that helps!

Categories

Resources