Friday, May 28, 2010

Re: BaseGenericInlineFormSet failing on Save As New in admin

Added this for my GenericInlineFormSet and things seem to work
correctly now:

class MyGenericInlineFormSet(BaseGenericInlineFormSet):
def __init__(self,*args,**kwargs):
self.save_as_new = kwargs.get('save_as_new',False)
super(MyGenericInlineFormSet,self).__init__(*args,**kwargs)

def initial_form_count(self):
if self.save_as_new:
return 0
return super(MyGenericInlineFormSet,
self).initial_form_count()

def total_form_count(self):
if self.save_as_new:
return super(MyGenericInlineFormSet,
self).initial_form_count()
return super(MyGenericInlineFormSet, self).total_form_count()

def _construct_form(self, i, **kwargs):
form = super(MyGenericInlineFormSet, self)._construct_form(i,
**kwargs)
if self.save_as_new:
# Remove the primary key from the form's data, we are only
# creating new instances
form.data[form.add_prefix(self._pk_field.name)] = None

# Remove the object id from the form's data
form.data[form.add_prefix(self.ct_fk_field_name)] = None
return form

On May 28, 12:36 pm, Stephen Sundell <stephen.sund...@gmail.com>
wrote:
> I'm using an InlineAdmin that is a generic relation, similar to django
> tagging, with one of my model admins.  The problem is on a save as
> new, if the original object has tags, then the initial form count,
> gotten from the management form, isn't zero, even though the new
> object will have zero tags, causing an error when the formset searches
> for those initial tags.  Now the BaseInlineFormSet takes care of this
> by returning zero for initial form count if save as new is set as
> True.  Is there a reason why BaseGenericInlineFormSet doesn't do the
> same thing?
>
> Hopefully I explained this well enough.
>
> Perhaps I'm missing something, but I think the
> BaseGenericInlineFormSet should do the same as the BaseInlineFormSet.
> I am currently using django 1.1, but looking at the code for 1.2, it
> seems to still so the same thing.
>
> Thanks in advance if you know what I'm doing wrong.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment