Django: How can I delete a formset entry if one of it's data is blank?
- by mkret
Hi,
I have the following scenario: I have a form with data that does not need translation and a formset with a textfield that should be translated into an undefined amount of languages.
Both parts are bound to a model. Each translated text is kept in a model with a foreign key that binds it to the untranslatable data. Something like:
class Person(models.Model):
name = models.CharField(max_length=60)
birth_date = models.DateField()
class PersonBio(models.Model):
person = models.ForeignKey(Person)
locale = models.CharField(max_length=10)
bio = models.TextField()
Each form in the formset has 2 fields:
A textfield (with the translated text)
A locale field (with the language into which the text was translated)
I've got it working with no problems until I tryed to change it's normal behaviour. I wanted to eliminate the need for the DELETE field by deleting an instance of the translated text if the textfield was left blank.
I've googled quite a lot now and read the whole documentation for forms, formsets and model validation but had no luck. To be honest, I couldn't even think of a solution.
Where should I implement this? On a Form clean() method? On the view? Somewhere in the Fieldset? Fieldset's save() method, maybe?
I'll keep trying to find a way to do that, but any help/tip/clue is appreciated.
Thanks in advance.