#27563 closed Cleanup/optimization (fixed)
Move "Apply limit_choices_to" code from BaseModelForm to fields_for_model()
Reported by: | Jon Dufresne | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.11 |
Severity: | Release blocker | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I frequently use fields_for_model()
to generate form fields dynamically on a model form. The form may have different fields based on the request, system settings, or other input. Using fields_for_model()
is very convenient to build these dynamic fields. For the most part, it is built consistently had the field originally been defined on Meta.fields
.
One shortfall I noticed, limit_choices_to
is not applied to form field querysets the way it typically is for ModelForm
s. To make the fields_for_model()
function more convenient and consistent with fields generated by ModelForm
, I'd like to suggest moving the "Apply limit_choices_to" code from BaseModelForm
to fields_for_model()
.
Currently here: https://github.com/django/django/blob/3507d4e773aa9ff2336e7230ba231c4ba6eb568f/django/forms/models.py#L294-L300
Suggested location: https://github.com/django/django/blob/3507d4e773aa9ff2336e7230ba231c4ba6eb568f/django/forms/models.py#L172-L173
Change History (13)
comment:1 by , 8 years ago
Has patch: | set |
---|
comment:3 by , 8 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
In 6abd6c598ea23e0a962c87b0075aa2f79f9ead36:
Fixed #27563 -- Moved "apply limit_choices_to" code from BaseModelForm to fields_for_model().
comment:4 by , 8 years ago
Has patch: | unset |
---|---|
Resolution: | fixed |
Severity: | Normal → Release blocker |
Status: | closed → new |
Triage Stage: | Ready for checkin → Accepted |
Version: | master → 1.11 |
Reopening per the regression reported in #27937.
comment:5 by , 8 years ago
It looks like something is wrong with ModelChoiceField.__deepcopy__(), it should be assigning self.queryset.all()
to make sure _result_cache
isn't shared between instances.
comment:9 by , 8 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:10 by , 7 years ago
This change breaks inheriting from django.forms.models.ModelForm directly.
I'm upgrading to python3.6 + django-1.11.2, and gets wrong with this commit. I'm using dynamic limit_choices_to function to get user specific filter expr, one of my function is like:
def limit_export_branch(): user = current_user() if user: bs = user.userprofile.branches_with_perms(['add_export', 'change_export']) return Q(pk__in=bs.values_list('pk', flat=True), businesses__name=BUSINESS_BROADBAND) return Q(pk=-1) # deny to avoid info leak
It just called once during develop server init, and won't be call while open a USER EDIT FORM, but it work in django admin edit page, by reading the code, django admin build form using modelform_factory(), with this commit reverted, my customized form works just fine. So, I think it's a regression.
Or, the usage of ModelForm changed? but newest doc not mentioned.
comment:11 by , 7 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
comment:12 by , 7 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
This change is released, so please open a new ticket at this point. I'm not sure if what you provided is sufficient to reproduce the issue. A sample project or a test would be helpful.
PR