Skip to content

Commit a54857c

Browse files
PiDelportblag
authored andcommitted
SlugModel: Avoid saving with force_insert twice
If the 'force_insert' flag was passed, don't pass it again: doing so will attempt to re-insert with the same primary key, which will cause an IntegrityError.
1 parent 9eefc8a commit a54857c

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

cities/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def save(self, *args, **kwargs):
4747
self.slug = 'invalid-{}'.format(''.join(choice(ascii_uppercase + digits) for i in range(20)))
4848
super(SlugModel, self).save(*args, **kwargs)
4949
self.slug = slugify_func(self, self.slugify())
50+
51+
# If the 'force_insert' flag was passed, don't pass it again:
52+
# doing so will attempt to re-insert with the same primary key,
53+
# which will cause an IntegrityError.
54+
kwargs.pop('force_insert', None)
5055
super(SlugModel, self).save(*args, **kwargs)
5156
else:
5257
# This is a performance optimization - we avoid the transaction if

0 commit comments

Comments
 (0)