Andy
Andy

Reputation: 50550

Chaining Foreign Keys

I have a few models that look like this:

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)

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 user_dashboard_group. I'm not sure how I can do this, based on my current models though. query and user_dashboard_group both have a foreign key to System, but not to each other. To get this though, I'd have to get the user_dashboard_group's system and then use that to filter the QueryData. How can I do this?

Edit 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.

Demo dashboard

I also realized that I didn't mention I was using django-smart-selects

Upvotes: 2

Views: 3928

Answers (2)

schacki
schacki

Reputation: 9533

I have never worked with django-smart-selects, but following the docs, I would expect

query = models.ChainedForeignKey(QueryData,**kwargs)

instead of

query = models.ForeignKey(QueryData)

since you want the options in query depend on the other selections. Is that understanding correct? If so, it was only about defining the right **kwargs.

For **kwargs, I would propose something like this

Update

chained_field='query', chained_model_field='user_dashboard_group__system__query_set__query'

assuming that the fields name is 'system in both cases. I am pretty sure that this describes the relationship correctly. I am just not sure, if django-smart-selects supports that syntax.

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Same way you would do it across one relation, except across two.

field__relation1__relation2

Upvotes: 1

Related Questions