Victor Hooi wrote:
> I tried that, and the fields aren't there, but when I try to save the
> object, I get a:
> 
>     IntegrityError at /admin/people/address/add/
>     people_address.content_type_id may not be NULL
> 
> so obvoiusly Django doesn't like it if those fields aren't filled.
it's a bug http://code.djangoproject.com/ticket/12577

> How do people normally do this sort of thing, with a polymorphic
> object that's referenced by multiple other objects?
use this hack until it's fixed

if 'django.contrib.contenttypes' in settings.INSTALLED_APPS:
    # patch django.contrib.contenttypes.generic.BaseGenericInlineFormSet
    # to provide the instance.pk
    # see http://code.djangoproject.com/ticket/12577
    from django.contrib.contenttypes import generic

    def save_new(self, form, commit=True):
        # Avoid a circular import.
        from django.contrib.contenttypes.models import ContentType
        kwargs = {
            self.ct_field.get_attname():
ContentType.objects.get_for_model(self.instance).pk,
            self.ct_fk_field.get_attname(): self.instance.pk,
        }
        new_obj = self.model(**kwargs)
        return generic.save_instance(form, new_obj, commit=commit)
    setattr(generic.BaseGenericInlineFormSet, 'save_new', save_new)

-- 
()_() | That said, I didn't actually _test_ my patch.      | +----
(o.o) | That's what users are for!                         | +---+
'm m' |                                   (Linus Torvalds) |  O  |
(___) |              raffaele dot salmaso at gmail dot com |
-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to