Code

Opened 2 years ago

Closed 17 months ago

Last modified 17 months ago

#17663 closed Bug (fixed)

Method "save" in BaseModelFormSet is not marked as alters_data

Reported by: aiv Owned by: vanschelven
Component: Forms Version: master
Severity: Normal Keywords: formset
Cc: anssi.kaariainen@…, lemaire.adrien@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description


Attachments (1)

save.patch (380 bytes) - added by aiv 2 years ago.

Download all attachments as: .zip

Change History (9)

Changed 2 years ago by aiv

comment:1 follow-up: Changed 2 years ago by akaariai

  • Cc anssi.kaariainen@… added
  • Easy pickings set
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Makes sense. I wonder if this kind of patch needs tests. We don't have tests for the existing .alters_data ones. Adding a test is of course easy, just check that the function's alters_data attr is True. So, my initial feeling is why not? I guess the right place is in regressiontests/model_forms_regress/tests.py

comment:2 Changed 2 years ago by Fandekasp

  • Owner changed from nobody to Fandekasp

This patch is not good, no path is given to the models.py. Notice 222 models.py exist in the project
(find . -name "models.py"|wc). Anyways, I found it after a grep, will now write the test.

comment:3 follow-up: Changed 2 years ago by Fandekasp

  • Cc lemaire.adrien@… added

@akaariai, tests for alters_data are in regressiontests/templates/callables.py

EDIT: I don't think we need alters_data for BaseModelFormSet.save(). This function will call each form.save(), and BaseModelForm.save() already have this alters_data = True.

I went through the formset fields with ipdb, and couldn't find a sensitive function.

Version 2, edited 2 years ago by Fandekasp (previous) (next) (diff)

comment:4 in reply to: ↑ 3 Changed 17 months ago by vanschelven

  • Owner changed from Fandekasp to vanschelven
  • Status changed from new to assigned

Replying to Fandekasp:

@akaariai, tests for alters_data are in regressiontests/templates/callables.py

EDIT: I don't think we need alters_data for BaseModelFormSet.save(). This function will call each form.save(), and BaseModelForm.save() already have this alters_data = True.

I went through the formset fields with ipdb, and couldn't find a sensitive function.

AFAIK the alters_data = True attribute does not "magically" bubble up through used method calls (like an exception would). I.e. if you create a method on the formset that calls other methods with alters_data = True, the former does not automatically have the correct value for alters_data. In other words, this is a perfectly valid concern.

>>> from django.forms.models import modelformset_factory
>>> MyFormSet = modelformset_factory(MyModel)
>>> fs = MyFormSet()
>>> fs.save.alter_data
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'function' object has no attribute 'alters_data'

comment:5 Changed 17 months ago by vanschelven

Just adding to this to proof that the regular case (ModelForm) does work as expected:

>>> class MyModelForm(ModelForm):
...     class Meta:
...         model = MyModel
...
>>> MyModelForm().save.alters_data
True

comment:6 in reply to: ↑ 1 Changed 17 months ago by vanschelven

Replying to akaariai:

Makes sense. I wonder if this kind of patch needs tests. We don't have tests for the existing .alters_data ones. Adding a test is of course easy, just check that the function's alters_data attr is True. So, my initial feeling is why not? I guess the right place is in regressiontests/model_forms_regress/tests.py

I've created a pull request for this (with the correct file location).
https://github.com/django/django/pull/750

As mentioned above, and in line with the current state of the tests, I've not added tests. I have, however, ran the existing tests and no new failures are caused by this patch.

comment:7 Changed 17 months ago by Klaas van Schelven <klaas@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 91c947066408d496f8990ba90fbf2c4d1d80f63f:

Method "save" in BaseModelFormSet is marked as alters_data

Fixes #17663

comment:8 Changed 17 months ago by Aymeric Augustin <aymeric.augustin@…>

In 7acabbb9800de4492c0ee612a8a0aa784eb07f49:

Merge pull request #750 from vanschelven/master

Fixed #17663 -- Marked BaseModelFormSet.save as alters_data.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.