Note that there are some explanatory texts on larger screens.

plurals
  1. POChaining Foreign Keys
    primarykey
    data
    text
    <p>I have a few models that look like this:</p> <pre><code>class System(models.Model): '''Defines a system''' system_desc = models.CharField('System Name', max_length=50) class SystemGroup(models.Model): '''Groups within a 'System' (ie. Customers, regions, divisions, etc. ''' group_desc = models.CharField('Group Name',max_length=25) system = models.ForeignKey(System) class QueryData(models.Model): '''Queries related to a 'System' (ie. Active users against System1, Orders today in system2, etc. ''' qry_desc = models.CharField('Query Description', max_length=50) system = models.ForeignKey(System) class UserDashboard(models.Model): '''User specific Dashboard ''' user = models.ForeignKey(User) board_name = models.CharField('Dashboard Name', max_length=50) class UserDashboardGroup(models.Model): '''Groups on the dashboard (ie. 'System 1's Key Indicators', 'System 2's Hot Sheet', etc. ''' group_desc = models.CharField('Group Display Title', max_length=50) user = models.ForeignKey(User) dashboard = models.ForeignKey(UserDashboard) system = models.ForeignKey(System) system_group = models.ForeignKey(SystemGroup) class UserGroupQuery(models.Model): '''Queries that run in a specific group on the dashboard (ie. Within 'System 1's Key Indicators, we run queries for active users today, orders in the last hour, etc. ''' user = models.ForeignKey(User) dashboard = ChainedForeignKey(UserDashboard, chained_field='user', chained_model_field='user', show_all=False, auto_choose=True) user_dashboard_group = ChainedForeignKey(UserDashboardGroup, chained_field='dashboard', chained_model_field='dashboard') query = models.ForeignKey(QueryData) </code></pre> <p>I am having problems with the very last part of this. I want to restrict the 'query' that appears in a admin page based on the selected <code>user_dashboard_group</code>. I'm not sure how I can do this, based on my current models though. <code>query</code> and <code>user_dashboard_group</code> both have a foreign key to <code>System</code>, but not to each other. To get this though, I'd have to get the <code>user_dashboard_group</code>'s <code>system</code> and then use that to filter the <code>QueryData</code>. How can I do this?</p> <p><strong>Edit</strong> I'm adding in a picture to (hopefully) describe a little better what I want to do. In step 1, the user inputs a name for this group of queries. This group is associated with a system (#2) and a predefined group within the system (#3) (think of #3 as a 'customer' or a 'region', etc and #1 and #3 are NOT the same thing, despite the similar naming). They then select 'Save and Continue editing' on this inline form and the drop down at step 4 becomes populated with information from the above form. Once step #4 has a selection made, I want #5 to populate with data only from the associated system. Since #2 contains this information, I am hoping it is fairly easy to do this, but I can't figure out the chaining. </p> <p><img src="https://i.stack.imgur.com/RnCKj.png" alt="Demo dashboard"></p> <p>I also realized that I didn't mention I was using <a href="https://github.com/digi604/django-smart-selects/" rel="nofollow noreferrer">django-smart-selects</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload