#22609 closed Uncategorized (wontfix)
Allow specifying the form field when instantiating a model field
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Uncategorized | Version: | 1.6 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I find myself having to do this time and time again, which really isn't DRY and won't be good for someone looking at my code in the future:
models.py class MyModel(models.Model): HOME_DELIVERY = ( (True, _(u"Home Delivery)), (False, _(u"Pickup (free)")), ) home_delivery = models.BooleanField(verbose_name="Pickup/Home Delivery", choices=HOME_DELIVERY, help_text="Something.", blank=False) forms.py class MyModelForm(forms.ModelForm): class Meta: model = MyModel home_delivery = forms.ChoiceField(empty_label=None, choices=MyModel.HOME_DELIVERY, widget=forms.RadioSelect(), label="Pickup/Home Delivery")
In this example, the customisation that I am looking for is to use a radio widget, and to remove the 'empty value' (defaults to '--------')
In order to do so I have to redefine the home_delivery form field, which means I have to redefine both the choices *and* the label.
If another developer comes along and changes the verbose_name for the model, they'll (might) be stumped as to why the form label isn't changing.
I propose being allowed to specify the form in the model, so I could replace all of this with:
models.py class MyModel(models.Model): HOME_DELIVERY = ( (True, _(u"Home Delivery)), (False, _(u"Pickup (free)")), ) home_delivery = models.BooleanField(verbose_name="Pickup/Home Delivery", choices=HOME_DELIVERY, help_text="Something", blank=False, field=forms.ChoiceField(empty_label=None, widget=forms.RadioSelect()))
Cheers
Change History (4)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
An even more annoying case I've come across now is where I just want to change the empty_label
for a ForeignKey
field. I have to go all the way and create a new form.ModelChoiceField
, redefine the queryset, etc. etc. just so I can specify an empty label:
models.py class MyModel: ... pickup_point = models.ForeignKey(PickupPoint, verbose_name="Pickup Point", blank=True, help_text="The Pickup Point to collect your box from.", null=True, related_name="box_orders") forms.py class MyModelForm: ... pickup_point = forms.ModelChoiceField(queryset=PickupPoint.objects.all().order_by('name'), empty_label="Select a pickup point", label="Pickup Point")
P.S. apologies for the multiple replies.
comment:3 by , 10 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I don't think the idea of specifying a form field on the model is going to fly since we don't want to add coupling like that between models and forms.
Consider using label=MyModel._meta.get_field('home_delivery').verbose_name
to make it more DRY.
I don't think pulling unspecified kwargs from the model would be backwards compatible as you might have options changing on existing model form fields that are redefined on the form.
I'm not saying the current situation is great, but I don't see a viable solution proposed here. Feel free to start a discussion on django-developers if you'd like to continue the discussion on how this can be improved.
comment:4 by , 10 years ago
OK, I agree that coupling models/forms isn't great (even though modelForm and models are already coupled right?)
I've come across quite a few limitations with subclassing/extending forms from models today so it's definitely something to discuss. Thanks
Perhaps another solution would be:
when creating the new form field, pull the unspecified kwargs from the model-created form field.
So: in MyModelForm, I wouldn't specify
choices
orlabel
, and form.ModelForm would know to look at the auto-generated form for these values (if it can find them)