You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use the form collections for a set of nested models and filter the queryset for relevant form widgets in the context of the correct "parent" foreign key object. For example using the testapp companies model, if I have a form collection view on some "Department" and department has a field "sales_team" to teams. Then I want to filter the options in the sales_team selection widget in the department form to only those teams in the same department.
To show what I mean, I have added a "sales_team" field to department, which points (nullably) to one of its own teams.
I tell the DepartmentForm about the queryset using the __init_method:
class Meta:
model = Department
fields = ['id', 'name', 'sales_team']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# we have current object so get any teams that are related to this department
if "instance" in kwargs and kwargs["instance"]:
self.fields["sales_team"].queryset = Team.objects.filter(department_id=kwargs["instance"].id)
# we don't have an instance so we are creating a new department
# so no teams to show
else:
self.fields["sales_team"].queryset = Team.objects.none()
And that works to limit the available sales teams to only those owned by that department:
That is filtered from the full list of teams:
However when this is switched over to using form collections, it stops applying the filters. The problem I am having with form collections is that the modelForms for the collection are initialized during import, so I can't use the "init" method to set the querysets for the widgets.
Like, this Form is created at package import time, so the form can't easily be given any instance data at that time:
The collections form version is finding the "teams" for the department:
I've been digging about in the code looking for the correct method to override.... can anyone give me a pointer
The text was updated successfully, but these errors were encountered:
but I don't see anything in the ModelForm instance that gets passed from the View.... other than the initial object. So maybe pass some extra values in that...?
Hi,
I am trying to use the form collections for a set of nested models and filter the queryset for relevant form widgets in the context of the correct "parent" foreign key object. For example using the testapp companies model, if I have a form collection view on some "Department" and department has a field "sales_team" to teams. Then I want to filter the options in the sales_team selection widget in the department form to only those teams in the same department.
To show what I mean, I have added a "sales_team" field to department, which points (nullably) to one of its own teams.
code
I tell the DepartmentForm about the queryset using the __init_method:
code
And that works to limit the available sales teams to only those owned by that department:
That is filtered from the full list of teams:
However when this is switched over to using form collections, it stops applying the filters. The problem I am having with form collections is that the modelForms for the collection are initialized during import, so I can't use the "init" method to set the querysets for the widgets.
Like, this Form is created at package import time, so the form can't easily be given any instance data at that time:
The collections form version is finding the "teams" for the department:
I've been digging about in the code looking for the correct method to override.... can anyone give me a pointer
The text was updated successfully, but these errors were encountered: