I've read quite a few posts on how to make a Database/Model unique in Django and it all seems to work. However, I do not see any posts discussing an efficient way of avoiding adding duplicate entries to a database.
My model looks like this:
# from app.models
class TestModel(models.Model):
text = models.TextField(unique=True, null=True)
fixed_field = models.TextField()
The way I currently avoid adding duplicate entries without getting an error is as follows.
# from app.views
posts = ["one", "two", "three"]
fixed_field = "test"
for post in posts:
try:
TestModel(text=post, fixed_field = fixed_field).save()
except IntegrityError:
pass
If I would not do this I would get an IntegrityError. Is there any way I could make this more efficient?