Skip to content

Instantly share code, notes, and snippets.

@anibalpacheco
Created May 19, 2021 04:33
Show Gist options
  • Save anibalpacheco/96f5537490adc782892da265332f7f8e to your computer and use it in GitHub Desktop.
Save anibalpacheco/96f5537490adc782892da265332f7f8e to your computer and use it in GitHub Desktop.
invalid choice error when deleting a TabbedInline in Django 1.5
def save_existing_objects(self, commit=True):
self.changed_objects = []
self.deleted_objects = []
if not self.initial_forms:
return []
saved_instances = []
try:
forms_to_delete = self.deleted_forms
except AttributeError:
forms_to_delete = []
for form in self.initial_forms:
pk_name = self._pk_field.name
raw_pk_value = form._raw_value(pk_name)
# clean() for different types of PK fields can sometimes return
# the model instance, and sometimes the PK. Handle either.
try:
pk_value = form.fields[pk_name].clean(raw_pk_value)
pk_value = getattr(pk_value, 'pk', pk_value)
obj = self._existing_object(pk_value)
if form in forms_to_delete:
self.deleted_objects.append(obj)
obj.delete()
continue
if form.has_changed():
self.changed_objects.append((obj, form.changed_data))
saved_instances.append(self.save_existing(form, obj, commit=commit))
if not commit:
self.saved_forms.append(form)
except ValidationError:
pass
return saved_instances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment