-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
model clean/full_clean is probably not called ? #526
Comments
@berycz +1 for this to be included. In the meantime I am trying to get around this with a A model where I override save to do a full clean before save and with class myModel(models.Model):
# model stuff
def clean(self):
super().clean()
# check for some conditions and raise
raise ValidationError()
def save(self, *args, **kwargs):
self.full_clean()
return super().save(*args, **kwargs) An class myEditTable(EditTable):
class Meta:
auto__model = myModel
columns__select__include = True
edit_actions__save__include = True
edit_actions__save__post_handler = my_post_handler
def my_post_handler(table, request, **_):
for row in table.selection():
print (row) # confirm that method is being called on save
print (type(row)) # confirm this is a django model instance
row.save() My expectation is that the Thanks |
I think I know why this doesn't work—the |
Some more on this. I think I can handle this use case with the def preprocess_rows_method(rows, **_):
for row in rows:
try:
yield row
except ValidationError:
raise This will correctly raise the ValidationError when the condition arises but will do so as a stack trace. The question is how to get this passed back cleanly in the response so the table displays the error instead of the stack trace. |
The post handler is not post-save, this is incorrect. You can check the code: https://github.com/iommirocks/iommi/blob/master/iommi/edit_table.py#L279
This part confuses me. From what I can see in the Django source code, Have I misunderstood this? |
Thanks, I'll try make it a little clearer. In my model I have added a class myModel(models.Model):
# model stuff
def clean(self):
super().clean()
# check for some conditions and raise
raise ValidationError()
def save(self, *args, **kwargs):
self.full_clean()
return super().save(*args, **kwargs) The intended behavior here is that The benefit is that the API endpoints, forms, and any Python code that creates and saves model class myModel(models.Model):
classification = models.CharField(max_length=50)
comment = models.TextField(null=True, blank=True)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# save the value of classification as __original_classification if the instance exists, else pass
try:
self.__original_classification = self.classification
except ObjectDoesNotExist:
pass
def clean(self):
super().clean()
# check for conditions and raise
if self.pk is not None:
if self.__original_classification != self.classification and (self.comments is None or self.comments == ""):
raise ValidationError(
_('Comments are required if any %(field)s value has been altered'),
code='invalid',
params={'field': 'classification'}
)
def save(self, *args, **kwargs):
self.full_clean()
return super().save(*args, **kwargs) So, when a user tries to change the value of The next step is to try to make this work with class myEditTable(EditTable):
class Meta:
auto__model = myModel
columns__select__include = True
edit_actions__save__include = True When I select records and save them, the correct This is expected given the model setup above. What I am unsure of is how to handle this in the Lines 281 to 282 in 5f1a440
as follows: try:
save(table.cells_for_rows(), table.edit_form)
except ValidationError as error_dict:
for _e in error_dict.messages:
print (_e)
table.edit_form.add_error(_e)
print (table.edit_form.get_errors()) I can confirm that the above will raise the exception and add the error to the table.edit_form, i.e.
What I am unsure of is how to return the
back to the view so that it is displayed with the selected I had originally posted here since I had mistakenly thought the I hope that is clearer. |
@wdelport-sgi I think what you are talking about is quite different from what this ticket is about. Calling |
Right, I agree that after exploration, this is not precisely the scope of this issue, but whether I'm calling try:
save(table.cells_for_rows(), table.edit_form)
except ValidationError as error_dict:
for _e in error_dict.messages:
table.edit_form.add_error(_e)
return with the following in the Template
|
https://docs.djangoproject.com/en/stable/ref/models/instances/#django.db.models.Model.clean
and it probably should be called?
The text was updated successfully, but these errors were encountered: