#26901: enable "save-as-new" feature for GenericInlines / ContentTypes
--------------------------------------+-------------------------
     Reporter:  eecp                  |      Owner:  nobody
         Type:  New feature           |     Status:  new
    Component:  contrib.contenttypes  |    Version:  1.9
     Severity:  Normal                |   Keywords:  save_as_new
 Triage Stage:  Unreviewed            |  Has patch:  0
Easy pickings:  0                     |      UI/UX:  0
--------------------------------------+-------------------------
 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/26901>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/047.d23604827d5f69aea7fc7230d5ee0cd5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to