@@ -218,6 +218,50 @@ def test_history_user_on_save_in_admin(self):
218218 [p .history_user for p in Poll .history .all ()], [self .user , self .user ]
219219 )
220220
221+ def test_history_change_reason_on_save_in_admin (self ):
222+ self .login ()
223+
224+ # Ensure polls created via admin interface save correct change reason
225+ change_reason = "New change reason"
226+ initial_data = poll_data = {
227+ "question" : "new poll?" ,
228+ "pub_date_0" : "2012-01-01" ,
229+ "pub_date_1" : "10:00:00" ,
230+ "history_change_reason" : change_reason ,
231+ }
232+ self .client .post (reverse ("admin:tests_poll_add" ), data = poll_data )
233+ poll = Poll .objects .get ()
234+ self .assertEqual (poll .history .get ().history_change_reason , change_reason )
235+
236+
237+ # Ensure polls modified via admin interface save correct change reason
238+ change_reason = "Edited change reason"
239+ poll_data = {
240+ "question" : "new poll?" ,
241+ "pub_date_0" : "2011-01-01" ,
242+ "pub_date_1" : "10:00:00" ,
243+ "history_change_reason" : change_reason ,
244+ }
245+ self .client .post (reverse ("admin:tests_poll_change" , args = [poll .id ]), data = poll_data )
246+ poll .refresh_from_db ()
247+ self .assertEqual (poll .pub_date .year , 2011 )
248+ self .assertEqual (poll .history .count (), 2 )
249+ self .assertEqual (poll .history .latest ().history_change_reason , change_reason )
250+
251+ # Let's emulate a revert
252+ change_reason = "Revert to history record 0"
253+ response = self .client .get (get_history_url (poll , 0 ))
254+ form = response .context .get ("adminform" ).form
255+ self .assertEqual (form ["history_change_reason" ].value (), None ) # Always starts empty
256+ self .assertEqual (form ["pub_date" ].value ().year , 2012 )
257+
258+ self .client .post (get_history_url (poll , 0 ), data = initial_data | {"history_change_reason" : change_reason })
259+
260+ poll .refresh_from_db ()
261+ self .assertEqual (poll .pub_date .year , 2012 )
262+ self .assertEqual (poll .history .count (), 3 )
263+ self .assertEqual (poll .history .latest ().history_change_reason , change_reason )
264+
221265 def test_underscore_in_pk (self ):
222266 self .login ()
223267 book = Book (isbn = "9780147_513731" )
0 commit comments