I have a customer manager for a Django model which overrides the create
method to also save some related objects:
class CustomManager(models.Manager):
def create(self, amount, user, description):
txn = self.get_query_set().create(user, description)
txn.budget_transactions.create(amount)
return txn
My question is: how do I mock the call to txn.budget_transactions.create
to raise an exception?
The budget_transactions
attribute of the txn
object is an instance of django.db.models.fields.related.RelatedManager
. Using mock.patch
to mock this class doesn't work as it is declared dynamically - it can't be imported directly.
Does anyone know how to do this?
mock.patch
in the conventional way doesn't work due to the dynamic declaration of RelatedManager classes. – Diablerie