Opened 8 years ago

Closed 8 years ago

#26901 closed New feature (duplicate)

enable "save-as-new" feature for GenericInlines / ContentTypes

Reported by: eecp Owned by: nobody
Component: contrib.contenttypes Version: 1.9
Severity: Normal Keywords: save_as_new
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hope i haven't overlooked a related ticket. I'm using Django 1.9.7 with some models having generic relations. Normal use like add or change with having those models as generic inlines in the admin of other models work without problem. However enabling "save_as" produces an database exception (content_types_id should not be not null) on executing the save (which would actually be an add with initial data). I tracked down the issue up to the definitions in "forms.py" of contenttypes.

If one compares the init of BaseGenericInlineFormSet with those of BaseInlineFormSet one finds that the save_as_new flag is not stored into self and later on there is no initial_form_count-method using this flag to return 0. This ultimately leads to the actual behaviour that the formset's save routine assumes that the freshly instantiated inline object seems to already exist in the database and therefore gets never assigned any content_types_id related to the "parent object".

I made a patch on my system like this:

@@ -15,6 +15,7 @@
                  prefix=None, queryset=None, **kwargs):
         opts = self.model._meta
         self.instance = instance
+        self.save_as_new = save_as_new
         self.rel_name = '-'.join((
             opts.app_label, opts.model_name,
             self.ct_field.name, self.ct_fk_field.name,
@@ -35,14 +36,24 @@
             **kwargs
         )
 
+    def initial_form_count(self):
+        if self.save_as_new:
+            return 0
+        return super(BaseGenericInlineFormSet, self).initial_form_count()
+

As i'm no hardcore coder i assume that maybe there is more to it and this patch should only be considered as partial.

Change History (3)

comment:1 by Tim Graham, 8 years ago

Might be a duplicate of #14642. If not, could you please provide a test for Django's test suite or a sample project to reproduce the issue?

comment:2 by eecp, 8 years ago

Indeed that looks very similar, sorry i missed that. My content_types_id error is reproducable with the model of simon@... So we can close this ticket. Still hope my patch lines may bring the topic a little forward.

comment:3 by Tim Graham, 8 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top