How to clean a model after the related models inline forms have been saved?

227 views
Skip to first unread message

Andy

unread,
Aug 26, 2018, 10:42:28 AM8/26/18
to Django users
I want to execute a check after the related data from the admin has been saved, but the default clean methods get executed before the formsets are saved. Where could i place my check to have access to a full version of what has been changed on the admin page?

Jason

unread,
Aug 26, 2018, 12:07:25 PM8/26/18
to Django users
why would you want to persist unvalidated data to your db?

Andy

unread,
Aug 26, 2018, 12:22:22 PM8/26/18
to Django users


Am Sonntag, 26. August 2018 18:07:25 UTC+2 schrieb Jason:
why would you want to persist unvalidated data to your db?


Well, i dont necessarily have to, but i somehow need to be able to perform a query with all updated data taken into account. The easiest solution i came up with is putting everything into the DB while in transaction so i could perform my queries and rollback if the checks dont work out. 

Im open to any other ideas.
And is there a prepared admin function that is suitable for validation logic after related objects are validated?
I might also just write some validation data to the parent while executing the formet validation and so i could verify my parent validation against this extra data even without additional queries against the DB.

So far i just see that the changeform_view handles object and formset validation without any way of interfering (other than rewriting that view logic).

Jason

unread,
Aug 26, 2018, 12:29:49 PM8/26/18
to Django users
I think we're missing a few things.  how fast does this query with updated values execute after the form submits? if you need to return the value back to the view, can't you do some sort of post-save retrieval and form populate?

Andy

unread,
Aug 26, 2018, 12:40:13 PM8/26/18
to Django users


Am Sonntag, 26. August 2018 18:29:49 UTC+2 schrieb Jason:
I think we're missing a few things.  how fast does this query with updated values execute after the form submits?

a neglectable amount of milliseconds
 
if you need to return the value back to the view, can't you do some sort of post-save retrieval and form populate?

No i dont want to return anything but a possible validation error based on all related objects new data. 
I probably see myself rewriting the changeform_view and manually adding the error the the forms non field errors, unless somebody comes up with a more clever idea.

Jason

unread,
Aug 26, 2018, 12:44:17 PM8/26/18
to Django users
I still don't understand why you want to persist unvalidated data to the db then?  if a user requests the data in mid-update, that's something that can be handled with transaction locks on the db.  so when that happens, the request will wait till the locks are released before executing the db query

Andy

unread,
Aug 26, 2018, 1:27:26 PM8/26/18
to Django users
Please forget about the persisting data part. 
The problem is that inside the parent form i dont have access to the related data and the formsets new related data is validated after the parent models data.
Inside the formset every related data model does not have access to the other related instances.

I want to have additional validation after all data on the admin page has been processed .. not necessarily saved into the database, but i need some way of doing additional validation after the basic stuff has been taken care of by the admin and its form.
Reply all
Reply to author
Forward
0 new messages