Reputation: 3097
I have been going through the documentation over and again. It says, i have to import django.forms to use multiple choice widget. I am confused, if I am defining League as Model, and want to choose DAY_CHOICES as multiplechoicefield, how would i go about doing it. Can i define calls forms inside a Model class?
For better understanding of my query, here is my models.py
class League(models.Model):
DAY_CHOICES = (
('MO', 'Monday'),
('TU', 'Tueday'),
('WE', 'Wednesday'),
('TH', 'Thursday'),
('FR', 'Friday'),
('SA', 'Saturday'),
('SU', 'Sunday'),
)
SEASON_STATUS = (
('In Progress', 'In Progress'),
('Concluded', 'Concluded')
)
LEAGUE_TYPE = (
('Home', 'Home League'),
('Away', 'Home and Away League'),
('Cup', 'Cup'),
('CupAway', 'Cup, Home and Away'),
)
name = models.CharField(max_length=30)
league_type = models.CharField(max_length=30, choices=LEAGUE_TYPE, default='Away')
play_days = models.CharField(max_length=28, choices=DAY_CHOICES, default='SU')
#defining rules for winning and loosing
win = models.IntegerField(max_length=2)
loss = models.IntegerField(max_length=2)
draw = models.IntegerField(max_length=2)
status = models.CharField(max_length=15, choices=SEASON_STATUS, default='In Progress')
//mouse
Upvotes: 0
Views: 1548
Reputation: 28637
typically a charField
with choices
is used to store a single choice.
Say you choose Sunday and Monday. What would you like to store in play_days
? Some kind of serialisation, e.g. "SU, MO"? Then you need to tell django how to serialise the data. Also, this is probably a bad idea, as it makes it difficult to query the data.
Instead, consider making a new class to hold days, and changing play_days
to a many to many field. As a bonus, m2m fields have a multiple choice widget by default
Upvotes: 0
Reputation: 2300
There are three different things that you really don't want to get confused in Django:
The model field controls what goes into the database, and when using the automatic form creation features, the model field type will give you a form containing a certain form field.
The form widgets control the HTML that the user interacts with in the form. For some form field types there might be more than one widget that could work (i.e. the time & date widgets admin uses versus the ones that are normally in user created forms).
Once you get this distinction clear in your mind then much of the documentation should make a lot more sense.
Upvotes: 2
Reputation: 2272
You do not configure forms within your models.py file. Your models.py file looks fine. Forms, and the widgets used to represent individual fields within those forms, are generally defined in a forms.py file. Look over the documentation on creating forms from models as well as the documentation on customizing the display of models within the Admin interface (assuming you're making this form available through that interface).
Upvotes: 0