ModelForm doesn't know how to handle One-to-One field
|Reported by:||Philipp Wollermann||Owned by:||nobody|
|Cc:||someone@…||Triage Stage:||Design decision needed|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Lets say I create a model using multi-table inheritance like this:
class WebApp(models.Model): name = models.CharField(max_length=128, unique=True) owner = models.ForeignKey(User) active = models.BooleanField(default=True) preconfig = models.TextField(blank=True) autoconfig = models.TextField(blank=True, editable=False) postconfig = models.TextField(blank=True) class Meta: ordering = ["owner", "name"] class DjangoWebApp(WebApp): appname = models.CharField(max_length=32)
Now I create a ModelForm for DjangoWebApp:
class DjangoWebAppForm(ModelForm): class Meta: model = DjangoWebApp
When I render this form, it has all fields from WebApp and also the "appname" field from DjangoWebApp - so inheritance seems to work fine. However, it also has a "webapp_ptr" field, which is the One-to-One field link from DjangoWebApp to its parent. Of course it doesn't make any sense to set it by hand, as it gets automatically handled by Django. :) Now, if I exclude that field:
class DjangoWebAppForm(ModelForm): class Meta: model = DjangoWebApp exclude = ("webapp_ptr",)
The form renders correctly and I can save it without problems. Everything works just fine :) I can exclude these pointers manually in my app, but I feel it shouldn't be necessary.
What's worse is, that newforms-admin also doesn't know how to handle this, so one can't edit multi-table inherited models using that.