Moving database from PMA to Django - python

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.

Related

Django model setup on existing Oracle table

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.

Django ORM multiple table data fetching

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.

ManyToManyField references the 'id' field when using a custom primary key

I am using a custom primary key for a model that has a few ManyToManyFields. When I update the model and add an object to a ManyToManyField (using add(new_object)), I get an error signifying that its looking up the primary key using the id field (which perhaps exists in the intermediary table, but not in the model).
psycopg2.DataError: invalid input syntax for integer: "TL98GK"
LINE 1: ...WHERE ("placedir_place_place_categ"."place_id" = 'TL98GK' A...
I have been searching on SO for a while but havent been able to zero in the exact issue. I guess I may have to use custom through table for ManytoManyFields (as a punishment for using custom primary key) but I honestly dont want to go down that route.
Using Django 1.10 and Python 3
It seems the migration doesn't detect Foreign Key type changes well. It's a known bug. A workaround is mentioned here (i.e to explicitly change the field type to varchar(32)).
However, you may need to do more (like updating the constraints etc. on the table) depending on your use case. (For those interested to go that route, here is one example case with corresponding migration code).
(p.s I just decided to not use a custom primary key on models with manytomany fields)

Django Create Foreign key to external table

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

Using 'old' database with django

I'm using a hand built (Postgres) database with Django. With "inspectdb" I was able to automatically create a model for it. The problem is that some tables have multiple primary keys (for many-to-many relations) and they are not accessible via Django.
What's the best way to access these tables?
There is no way to use composite primary keys in Django's ORM as of now (up to v1.0.2).
I can only think of three solutions/workarounds:
There is a fork of django with a composite pk patch at github that you might want to try.
You could use SQLAlchemy together with Django.
You have to add a single field primary key field to those tables.
Django does have support for many-to-many relationships. If you want to use a helper table to manage this relationships, the ManyToManyField takes a through argument which specifies the table to use. You can't model anything terribly complex this way, but it is good for most simple applications.

Categories

Resources