[Django]-Django: Mock a field on a model

-5👍

You can’t really bypass that check. The closest you can get is:

class SomeModel(models.Model):
    user_id = models.IntegerField(max_length=10)
👤lprsd

9👍

You can use unittest.patch to mock a property:

class TestThing(django.test.TestCase):
    @unittest.patch('my_app.models.SomeModel.user', new_callable=unittest.PropertyMock)
    def test_thing(self, user_field):
        user_field.return_value = 5
        ...

This works on Django fields.

Leave a comment