Reputation: 55
I want to write a custom field that shows size of an item. like: "small", "medium", "large", "extra large". It have to be optional like IntegerField. this is what I want it to be. I don't know where and what should I do.
Upvotes: 1
Views: 220
Reputation: 476624
You do not need to define a custom Django model field for that. In fact Django already can handle that. You can provide a choices=…
parameter [Django-doc] for that:
class SomeModel(models.Model):
SIZE_CHOICES = (
(1, 'small'),
(2, 'medium'),
(3, 'large'),
(4, 'extra large'),
)
size = models.IntegerField(choices=SIZE_CHOICES)
In a ModelForm
you can then use a RadioSelect
widget [Django-doc] to select out of the choices:
class SomeForm(forms.ModelForm):
class Meta:
model = SomeModel
fields = ('size',)
widgets = {
'size': forms.RadioSelect
}
Upvotes: 2