Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#7335 closed (fixed)

ModelForm doesn't know how to handle One-to-One field

Reported by: philwo Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: someone@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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

Attachments (0)

Change History (5)

comment:1 Changed 6 years ago by Berry <berry.groenendijk@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Yesterday I ran into exactly the same problem.

comment:2 Changed 6 years ago by Michael Placentra II <someone@…>

  • Cc someone@… 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 Changed 6 years ago by ericholscher

  • milestone set to 1.0
  • Triage Stage changed from Unreviewed to Design decision needed

comment:4 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

[8469] should have fixed this; please reopen if it didn't.

comment:5 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.