Hi,
I am trying to use the inlineformset_factory on an odd model I have
created. Here's the best way I can describe it:
class Base(models.Model):
title = models.CharField(max_length=55)
def save(self, *args, **kwargs):
if self.is_editable():
# do a bunch of things
self.pk = None
super(Base, self).save(*args, **kwargs)
class Inline(models.Model):
base = models.ForeignKey(Base)
Basically, Base will always create an entirely new instance of itself,
unless the is_editable returns false. This is basically a cheap way of
having a true "version history" of the entire model. It works, until
we get to my foreign key relation.
In my view, I have a simple:
InlineFormset = inlineformset_factory(Base, Inline, extra=0, fields
('id', 'body',))
if request.method == 'POST':
new_base = form_base.save() # a simple modelform, this is.
inline_formset = InlineFormset(request.POST, instance=new_base)
if inline_formset.is_valid():
inline_formset.save()
As soon as I hit the save(), I get a KeyError: None
Traceback shows it's happening within the core saving function within
django's model.py:
/home/bartek/.virtualenvs/tinyescrow/lib/python2.6/site-packages/
django/forms/models.py in save_existing_objects
# Put the objects from self.get_queryset into a dict so they
are easy to lookup by pk
existing_objects = {}
for obj in self.get_queryset():
existing_objects[obj.pk] = obj
saved_instances = []
for form in self.initial_forms:
print form.cleaned_data, self._pk_field.name
obj = existing_objects[form.cleaned_data
[self._pk_field.name]] ...
if self.can_delete and form.cleaned_data
[DELETION_FIELD_NAME]:
self.deleted_objects.append(obj)
obj.delete()
else:
if form.changed_data:
self.changed_objects.append((obj,
form.changed_data))
So basically, I'm not sure why I would get this KeyError. I am certain
it has something to do with what I'm doing with the Base models
primary key but I am not sure how to get around it. I've used
inlineformset_factory successfully on normal models without weird pk's
so it's a great function, just need to figure this out :)
Thanks in advance for any help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---