Issue 25493 question

Hello there!

I’ve ran into an error during work where I was sending a mock instance to an object creation and the error was difficult to identify/understand! I noticed there is an issue created for this in the tracker: #25493 (Model instances created with unittest.mock can raise confusing errors) – Django. But not sure what would be a proper solution! Any idea/suggestion? Just trying to get a brainstorming session here! Thank you so much!

Hi @mgaligniana,

Looking at the issue,

ValueError: Failed to insert expression "<MagicMock name='create().id.resolve_expression()' id='139633074508184'>" on fundraising.Payment.stripe_charge_id. F() expressions can only be used to update, not to insert.

It seems that you are trying to use the F() expression which is mostly used to update existing models’ fields. Kindly modify that or provide more information about the code that is creating this issue.

Hi! Thank you mawaki for your response!

Tim added an example to replicate it: 25493-test.diff on Ticket #25493 – Attachment – Django

I suggested this solution: #25493 (Model instances created with unittest.mock can raise confusing errors) – Django but not sure if it would add too much complexity to the method