Django ORM multiple table data fetching - python

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.

Related

Reverse foreign key relation in Django ORM

How to return all objects instances of a particular model that are being addresed as Foreign key by ANY object instance of a different model ? Lets say there is a model Item and a model ItemRequested that has foreign key relation to Item. How to print all Items that are being mentioned as foreign key in the ItemRequested table/model ? basically this is the SQL query that i want to execute in Django:
select * from backend_item where id in (select id from backend_itemrequested);
Obviously i want to avoid executing raw SQL commands from inside Django ORM
I'm not sure exactly what you're asking here. But perhaps this is what you want:
Item.objects.exclude(itemrequested=None)

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.

significance of Django Default tables

Django comes with default tables like AuthGroup, AuthGroupPermissions , AuthPermission, AuthUser, AuthUserGroups, AuthUserUserPermissions, DjangoAdminLog, DjangoContentType, DjangoSession and DjangoSite. What is the significance of each table?
I know that these tables comes from the apps included in the settings.py file, but I really dont understand the need to use some of the following tables above, such as Permissions and Groups. Where will I exactly use these tables?
The significance of the tables:
AuthGroup: Contains your groups, just id and name
AuthPermission: Contains the permissions of your project id, codename and a ForeignKey to the ContentType (Model) they belong to
AuthGroupPermissions: Table to keep the many to many relation between AuthGroup and AuthPermission (which permissions each group has)
AuthUser: Your users - username is the primary key
AuthUserGroups: Table to keep the many to many relation between AuthGroup and
AuthUser (which users belong to each group)
AuthUserPermissions: Table to keep the many to many relation between AuthUser and AuthPermission (which permissions each user has)
DjangoAdminLog: Records actions (insert/delete/update) your admin users do
DjangoContentType: Contains the content types of your project -- a content type is actually a Model in general - more info here https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
DjangoSession: Contains session information (session key, data and when it expires), more info here https://docs.djangoproject.com/en/dev/topics/http/sessions/
DjangoSite: Contains the sites your application can be used on - more information here: https://docs.djangoproject.com/en/dev/ref/contrib/sites/
Now, you answer your other question, you don't actually need to use these tables yourself. You will use the django ORM to create Users, Groups, Permissions etc and these tables will be updated through the ORM.

Moving database from PMA to Django

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.

sorting on foreign key columns in django admin?

I have two tables : customers and attributes.They are related as customers.id=attributes.id (for a given id joining the info of customers and attributes one can get the whole profile of the customer).In the Django admin panel of attributes, I want fields from both tables to be shown,search on any field that i wish and to be able to freely sort on any field.I have provided this foreign key relation in my models.py:
id=models.ForeignKey(customers)
I am able to display any columns that i wish from both the tables (using list_display),and also search on fields from the foreign table.However, the ability to click the title and sort on any column works only on the native fields,not the fields generated from the foreign table.
I also want to show a sum() result of a particular column at the top of the admin interface,like the search bar.
Is this possible in django?
did you try this one?
attributes = Attributes.objects.fi.select_related("event_def", "location", "space")

Categories

Resources