I have a model where because of a code bug, there are duplicate rows. I now need to delete any duplicates from the database.
Every row should have a unique photo_id. Is there a simple way to remove them? Or do I need to do something like this:
rows = MyModel.objects.all()
for row in rows:
try:
MyModel.objects.get(photo_id=row.photo_id)
except:
row.delete()
The simplest way is the simplest way! Especially for one off scripts where performance doesn’t even matter (unless it does). Since it’s not core code, I’d just write the first thing that comes to mind and works.
Use
.reverse()to delete the duplicates first and keep the first instance of it, rather than the last.As always, back up before you do this stuff.