I am using django 1.9 and one of my models has a many-to-many field. For example:
class PhoneNumber(models.Model):
number = models.CharField(max_length=255)
...
class Person(models.Model):
name = models.CharField(max_length=255)
phone_numbers = models.ManyToManyField(PhoneNumber)
...
(please ingnore the fact that I am saving the number as a CharField).
I want to write unit test for a serializer that I created with Django Rest Framework. For this purpose, I want to create instances of Person without relying on the database. I tried creating the object by
person = Person(...)
but I get the following error:
object needs to have a value for field “…” before this many-to-many relationship can be used
Is it possible to mock model instances without relying on first writing the instance to the database?