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")
Related
I have a wagtail page with an orderable field, however, I need to query from the API based on the value of an orderable. So for example, a manufacturer page has a field for products, which is a one to many relationship, and I want to get all manufacturers that make a particular product using the API
When you specify the related_name on the ParentalKey, you're setting up a relation that you can query using Django query expressions. For your example, if your orderable's ParentalKey has related_name='products, this would be something like:
ManufacturerPage.objects.filter(products__name='teapot')
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.
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.
I have two tables with a many-to-many relationship between them. One table holds types of properties, the other holds regions.
For the sake of simplicity let's say it goes something like this:
_property_
id
name
description
_region_
id
name
_property-region_
id
property.id
region.id
I want to build a form to create new properties, and I want this form to contain a multi-select option allowing the user to specify which region/s the property is available in.
I understand that I can create a multi-select form using list:reference without actually creating a junction table, but I would be interested in learning how to do this without de-normalising the database.
its not denoramalising?
each region can have many properties and relation is held by region...
on the query level you just ask for regions which hold the id of the property... (either way you have to query db for that even if you have junction/through table)
If you have specific info about relation, you have to process that logic yourself. Aint that hard,
create two forms validate property info and the record will be in database thus you posess the id. Then just add that id as one part of relation and all the selected items as other.
I try to create a related field on OpenERP 6.0.1 . is it possible to define two different onetomany relationfor the same field name? What all changes i must do in the(.py file and XML Files).
No you cannot do that:
the field names are keys in a Python dictionary, in what you write the second invoice_line will overwrite the first one
this would mess up OpenERP's ORM anyway as it does not handle relations to different tables.
So you need two different columns, one relative to account.invoice.line and the other to account.service.line. If you really need a merged view, then you can add a function field which will return the union of the invoice and service lines found by the two previous fields. But I'm not sure the forms will be able to handle this.