Forms and _ptr field

171 views
Skip to first unread message

Matt Fox

unread,
Apr 25, 2011, 7:37:19 PM4/25/11
to django-polymorphic
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

Tribaal

unread,
Apr 26, 2011, 7:25:14 AM4/26/11
to django-po...@googlegroups.com
Hi Matt,

I personally use the exclude = ("*_ptr") trick, which is the most simple as far as I can tell.

Nice to see some activity on this mailing list... I'm getting pretty frustrated that the library seems to work so well but the author is apparently totally inactive since a few month ago.

Cheers,

- Chris

Matt Fox

unread,
Apr 27, 2011, 11:16:15 AM4/27/11
to django-polymorphic
Thanks.

Is that technique documented anywhere? I'll suggest it to the author
via e-mail and see if he's still reachable.

On Apr 26, 4:25 am, Tribaal <trib...@gmail.com> wrote:
> Hi Matt,
>
> I personally use the exclude = ("*_ptr") trick, which is the most simple as
> far as I can tell.
>
> Nice to see some activity on this mailing list... I'm getting pretty
> frustrated that the library seems to work so well but the author is
> apparently totally inactive since a few month ago.
>
> Cheers,
>
> - Chris
>

Tribaal

unread,
Apr 27, 2011, 11:33:01 AM4/27/11
to django-po...@googlegroups.com
Nope, not as far as I know.

The author doesn't answer emails since a while now, so I took the maintainership of the package as long as we can't contact him. I started by uploading the current codebase to pypi. The now most advanced branch of django_polymorphic is therefore my branch on github (changes are only packaging-related for the time being).

https://github.com/chrisglass/django_polymorphic

While I certainly can't claim the project's lead or anything, I'll be glad to let my branch serve as a centralisation point should people want to commit code to whatever will be deployed to pypi. That is, until the author is reachable again (hopefully).

Hope this helps!

- Chris
Reply all
Reply to author
Forward
0 new messages