user2086641
user2086641

Reputation: 4371

how to save radio buttons values in db in django

forms.py

Date_Format = (
    ('0', ' dd / mm / yyyy'),
    ('1', 'mm / dd / yyyy'),
)

Time_Format = (
    ('0', ' 12 hour AM / PM '),
    ('1', ' 24 hour '),
)

class DateFormatForm(Form):
    date_format = forms.ChoiceField(choices=Date_Format,widget=forms.RadioSelect())

class TimeFormatForm(Form):
    time_format = forms.ChoiceField(choices=Time_Format,widget=forms.RadioSelect())

models.py

class Settings(models.Model):
    user = models.ForeignKey(User, null=True)
    date_format = models.CharField('Date format', max_length=100)
    time_format = models.CharField('Time format', max_length=100)
  1. I had kept the two forms namely,DateFormatForm and TimeFormatForm in template for selecting the date ad time formats.

  2. In models their are two field namely, i.date_format and ii.time_format for which forms choices are created.So if the user clicks this format ('0', ' dd / mm / yyyy'), and press save,the corresponding value is "0" which should be saved in date_format.

3.So date formats are saved in their appropriate fields with "0"'s or "1".

I can't use forms form validation and save because i am not using modelsForm,So how to save it in database.

Please share me a slight idea to perform this with out form.

Thanks

Upvotes: 1

Views: 1471

Answers (1)

Leandro
Leandro

Reputation: 2247

I think that your model could be refactored and you should use Django ModelForm.

But, why do you want to save the human-readable part in db if you have hardcored them in your code? do you want get it after?

if you want to get the human-readable part, just use get_date_format_display() for DateFormat model and get_time_format_display() for TimeFormat model. Django generates these methods

see this Documentation

Hope this helpsvalue

Upvotes: 1

Related Questions