Hi and thanks for this useful package, Bert.
I integrated django-polymorphic into my codebase, changing my forms to
something like:
class PlaceForm(forms.ModelForm):
...fields...
class Meta:
model = Place # Place is a PolymorphicModel
class RestaurantForm(PlaceForm):
class Meta:
model = Restaurant # Restaurant concretely extends Place
However, when calling is_valid() on my RestaurantForm object, Django
would then throw "AttributeError: can't set attribute" because it was
trying to set place_ptr on the restaurant object, which is a property
with no setter defined.
Surely I'm not the first one to come across this. What's the best way
to handle it? I can think of a couple solutions:
1. Add exclude = ("place_ptr",) to RestaurantForm.Meta class. (Got
this idea from a three year old Django bug report:
http://code.djangoproject.com/ticket/7335). I can confirm this works
and is probably the simplest solution, but I don't like it because you
still get AttributeError if you try to set the *_ptr field in your
code. A new PolymorphicForm class that did this automatically would be
useful.
2. In addition to creating the special accessor for *_ptr, create a
setter. For example:
def create_setter_function_for_model(model, setter_name):
def setter_function(self, obj):
pass
return setter_function
And apply it with:
setattr(self.__class__, name,
property(create_accessor_function_for_model(model, name),
create_setter_function_for_model(model, name)) )
Obviously that's a poor implementation of setter_function()- but it
seems to work too.
So how do most people handle this? Did I just miss something in the
docs?
Thanks,
Matt Fox