I have two related (via foreignkey relation) models and created admin model for parent with inlines. In several cases (edited in admin by boolean field), i need to delete all previous objects (inlines) and create new ones. I've tried to make it with save_model, where i can get all current object's properties and decide if i need to recreate (inline) objects. models:
class Model1(models.Model):
reformat = models.BooleanField(default=False)
...
class Model2(models.Model):
model1 = ForeignKey(Model1, related_name='model2')
...
admin:
class Model2Inline(admin.TabularInline):
model = Model2
class Model1Admin(admin.ModelAdmin):
inlines = [Model2Inline]
def save_model(self, request, obj, form, change):
super(Model1Admin, self).save_model(request, obj, form, change)
if obj.reformat:
obj.model2.all().delete()
# creating new objects
...
obj.save()
But if i try to delete these objects in model_save method i get ValidationError. Is there other possibilities to solve this problem?