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.
Related
Is there a way to migrate all tables from an oracle read-only database to django?
So basically I don't want to make any modification to my database. I just want to extract information from it.
From what I found till now, is a way by using routers but I don't know how exactly to use it.
Thanks, any help will be appreciated
DB: oracle
Django version: 2.2.12
python: 3.6
cx-Oracle: 7.3.0
you should use model.Meta.managed=False
you will define a normal model and set this attribute in it's meta
class AModel(models.Model):
field1 = ...
field2 = ...
class Meta:
managed = False
db_table = "name_of_already_created_table"
if by router you mean Database routers it should only be used if you have more than one database to work with.
you should write your models based on the tables and columns that you have in your oracle db and add the managed=False to each of them separately.
so if you have for example three tables in your database you will write three separate models for it in your models.py file. this way django wont create the tables and just trys to read those tables also you need the db_table attribute and value of it should be the name of your table in the database
I have an old table in the database. And I want to create a model in Django application.
After creating a model and I used migrate command then it created a new table with its own name.
Django provides a utility to auto-generate models from an existing database via inspectdb command.
You can create models by introspecting an existing database by executing following command
python manage.py inspectdb
The above command will output all the models Django can create from the existing database to stdout. You can save this as a file by using standard Unix output redirection
python manage.py inspectdb > models.py # or pass the app_name.models.py if you want to generate them inside models.py file of specific app
The output file will be saved to your current directory. Move that file to the correct app and you have a good starting point for further customization.
you can refer https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-inspectdb for more information.
You can specify the table name by setting table on the model's Meta class. Set managed = False to prevent Django from creating the table.
class ExistingModel(models.Model):
...
class Meta:
table = 'existing_table'
managed = False
After making these changes, I would revert the previous migration, remove the migration file, then run makemigrations again.
I want to view Django default User table from MySQL console.
I know to access from django shell or python by simply importing it.
from django.contrib.auth.models import User
is there a way to view it from MySQL console itself?
and where it will be located? i mean in which database django user table belongs?
Of course you can see it in the database.
As with all other models, unless instructed otherwise by using a db_table property in the Meta class, Django uses the naming schema appname_modelname for tables - so in this case the table is auth_user.
If you don't know and can't find the source, you can ask the model itself - ie User._meta.db_table.
The tables are located in the database which you have specified in settings.py. The django user table will be located at yourdbname.auth_user. All user defined models will be stored as yourdbname.appname_modelname
You can use "python manage.py dbshell" (in order to do this in linux, you need to install mysqlclient) and use "show tables;" and "select * from yourdb.table"
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
Scenario
I have a basic Django app in which users (django's authentication built in model) and reports have a many-to-many relationship.
The Problem
Django does not create a corresponding table to handle this relationship. My application is called reports. There is an error in the admin system upon trying to create a report and assign users to it. It tries to query the table reports_report_users and it fails as it does not exist.
models.py code
from django.db import models
from django.contrib.auth.models import User
class Report(models.Model):
name = models.CharField(max_length=30, blank=False)
users = models.ManyToManyField(User, related_name='reports')
def __unicode__(self):
return self.name
Attempted Solutions
Used this link as a reference: https://docs.djangoproject.com/en/1.4/topics/db/examples/many_to_many/
Ran manage.py syncdb about 300 times - ok, only once, but there were no errors and upon inspecting the SQLite db there were no additional tables created :(
It seems like you've added to the Report model after the first sync. Thus you're dealing with a migration, which django doesn't do natively.
First, Inspect the sql output, make sure that the create table instruction for your many to many relationship is there.
python manage.py sqlall
Assuming the problem is that this is a migration, which django doesn't handle natively, you've got three options:
1) Delete all db tables for this app, then run syncdb again.
2) Manually create the tables (fairly easy to copy paste the create sql from the sqlall command)
3) Start using a migration framework like South.
In the long run you'll appreciate the investment in learning south. In the short term, deleting the DB file is the fastest.-
Have you deleted your db file and run manage.py syncdb again?