I have a django app which works with a subset of tables within a database. What I want is to create a foreign key to a table which is not managed by the django app, reason is that I want to cascade on delete.
Is it possible to declare a foreign key in my django model to a table which is not controled by my django app?
Something like:
user = models.ForeignKey(table='external_table_name',field='user_id',
on_delete=models.SET_NULL)
You need to set meta options for your model to:
managed=False
Related
I'm using postgresql database in my django project.
I have multiple apps in my projects.
users/
UserProfile model
myapp/
CustomModel model
Now I need UserProfile table should be created in public schema
And CustomModel table needs to be created in a separate schema called myapp
How to implement this and Do I need to change anything in the queries or migration command in future after implementing this?
Just use a meta information:
class User(models.Model)
class Meta:
db_table = 'schema"."tablename'
I've been using it from some time and found not problem so far.
More info:
This will replace table name in all your database queries with db_table. So any query will SELECT * FROM "tablename" will be converted to SELECT * FROM "geo"."tablename". Its just a neat trick, hopefully Django gives this option natively in future.
I have a particular situation which I need help clarifying.
I have an existing Oracle table with an auto increment ID as a primary key
I am creating a django model to sync with that table so i can make use of django's ORM methods such as save(), filter() etc.
I read from the django docs the .save() method can perform both a UPDATE and INSERT depending on if the values passed to the primary key results in a True value (i.e. not a None or null).
In my table I have two columns which together will form a composite primary key.
If I specify primary_key = True on the two attributes on the django model, do I need to remove the primary key tag from oracle table?
Also, do i need to specify the unique_together to tell the django model that they are unique or will it be able to derive the index i created in the django oracle table?
Thanks.
I am new to python and Django as well. I am trying to use Django ORM for fetching data from database but i am unable to do this. My database has three tables :USER,INVESTMENT_NAME and WALLET. The columns in user table are id(primary key) and name, columns in investment_name table is id(primary key) and name and the wallet table has user_id(foreign key references id in user table) , inv_id(foreign key references id in investment_name table) ,date, quantity,amount and current price.
I am having trouble to fetch data which displays user name ,investment name,date,quantity,amount,current price using DJANGO ORM as i dont know how to do for multiple tables.
Any suggestions please.?
Do the Django ORM query on the Wallet table. Since there is a foreign key reference to both the other tables in this table, you can then access the entities using getters. Eg -
w = Wallet.objects.all()
w[0].user.name # This is the username
w[0].investment.name # This is the investment name
w[0].date # This is the wallet date
Use the Django's official documentation for more understanding on orm queries and how you can optimize it.
You should use doulble underscore to get table's ForeignKey (One-To-Many) relationship fields and related_name (field_name_set by default) to access to Many-To-One (ForeignKey that references the table) objects. There are docs: Lookups that span relationships
It's also good for perfomance to use select_related and prefetch_related.
I have an existing MySQL database that I set up on PMA, it has FKs that references columns that are not primary keys. Now I am trying to move the database to Django and am having trouble because when I try to set up d Foreign Keys in django it automatically references the Primary Key of the table that I am attempting to reference so the data doesnt match because column A and column B do not contain the same info. Is there a way to tell django what column to reference?
You can use the to_field attribute of a ForeignKey.
Django should detect this automatically if you use ./manage.py inspectdb, though.
use foreign fields in list_filter of django admin page
Suppose i have models
class Company():
name varchar(50)
field1 varchar(50)
class Client()
name varchar(50)
company ForeignKey(Company)
Now how can i implement filter on Client model for field1 in django admin page
I tried list_filter = ['company_field1', 'company_name'] but didn't work. Any solutions?
I'm using django 1.2 version.
Thanks in advance
This can't be done in Django 1.2.
Starting with 1.3 you can use that standard `relatedModel__field' syntax (see the docs). Note that you have to use a double underscore for this to work.
In the current dev version you could to even more complex things be using a subclass of SimpleListFilter which pratically allows you to do anything you like ;)
With Django 1.2, there is a hacky work around I've used for this sort of thing.
In your database, define a view on a join of the two tables with the foreign key relationship. Then make Django use the view instead of the real table. Put triggers on the view to redirect database writes to the correct table.