Set an initial sequence value automatically
masterTo prevent conflicts with existing data in a database, you can override the _setup_next_sequence class method in your factory. This method is called automatically upon the first instantiation of the factory. A common pattern is to query the database for the current maximum ID and return that plus one.
class AccountFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Account
@classmethod
def _setup_next_sequence(cls):
try:
return models.Accounts.objects.latest('uid').uid + 1
except models.Account.DoesNotExist:
return 1