#7335 closed (fixed)
ModelForm doesn't know how to handle One-to-One field
Reported by: | Philipp Wollermann | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | ||
Cc: | someone@… | Triage Stage: | Design decision needed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Best regards,
Philipp
Change History (5)
comment:1 by , 17 years ago
comment:2 by , 17 years ago
Cc: | added |
---|
I have the same problem. In admin, it shows the whatever_ptr field that should be handled automatically.
This appears also in the trunk version. However, in the trunk version, if you just ignore it, it works fine. When adding an object (in the trunk version), if you leave it set to nothing, it creates the object in the parent table automatically. When updating, it is set how it needs to be and doesn't cause problems (if I use that patch that sloonz uploaded here). When deleting, it seems to work OK but the parent object is not actually deleted from the database.
In the newforms-admin branch, the behaviors are all the same except that creating an object fails because it does not just create the parent object with the ..._ptr field set to nothing (you can update and delete if it's already there -- in my case, it was there because I entered it before switching to the newforms-admin branch).
comment:3 by , 16 years ago
milestone: | → 1.0 |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:4 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
[8469] should have fixed this; please reopen if it didn't.
Yesterday I ran into exactly the same problem.