0

Good morning pls i wanted to know how i can make radio button on django models

How to use models.RadioSelect()

30th May 2021, 7:50 AM
Benjamin
Benjamin - avatar
16 Answers
0
Benjamin please let me know if it worked for you. I will be happy to know🙂
1st Jun 2021, 5:58 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 2
In models.py in your model ======================= gender = models.CharField(max_length=6) #you can adjust by yourself In forms.py =========== GENDER_CHOICES =( ('male','Male'), ('female','Female') ) gender = forms.ChoiceField(choices=GENDER_CHOICES, widget=forms.RadioSelect())
30th May 2021, 8:40 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 2
Benjamin *If you don't have forms.py, then currently which method are you using to render form? * And are you using a custom model? * and are you using forms differently or you are using model forms? If you are using modelFrame: In models.py in your model ======================= GENDER_CHOICES =( ('male','Male'), ('female','Female') ) ... gender = models.CharField(max_length=10,choices=GENDER_CHOICES) class CustomForm(forms.ModelForm): class Meta: model = Your_Model widgets = {'gender': forms.RadioSelect}
30th May 2021, 9:53 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 2
Benjamin the error you posted have reached the word limit, I can't see the last line, can you please resend only the last line because only that line contain main error. in your code, try this: """ from django.db import models Exercise_choice = ( ('never', 'Never'), ('1 - 2 days', '1 - 2 Days'), ('3 - 4 days', '3 - 4 Days'), ('5 + days', '5 + Days'), ) class Exercises(models.Model): Exercise = models.CharField(choices=Exercise_choice, max_length=10, default='never') """" Whenever we define the list of choices, we give a tuple with 2 values: 1st one is which will stored in db and use by django and 2nd will be displayed in html only.
2nd Jun 2021, 2:43 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 1
AKSHAY🇮🇳 File "C:\Users\user\anaconda3\lib\site-packages\django\db\migrations\loader.py", line 118, in load_disk raise BadMigrationError( django.db.migrations.exceptions.BadMigrationError: Migration Form in app blog has no Migration class
2nd Jun 2021, 4:07 AM
Benjamin
Benjamin - avatar
+ 1
AKSHAY🇮🇳 thanks a lot for helping me I really appreciate
2nd Jun 2021, 4:08 AM
Benjamin
Benjamin - avatar
+ 1
Benjamin Welcome😊 I think you have forgotten to apply migrations. Please run following commands: python manage.py makemigrations python manage.py migrate
2nd Jun 2021, 4:20 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
0
AKSHAY🇮🇳 I would create forms.py by my self right?
30th May 2021, 8:43 AM
Benjamin
Benjamin - avatar
0
AKSHAY🇮🇳 this is in models.py # for Exercise in form.py Exercise = models.CharField(max_length=100) # for Eating in form.py Eating = models.CharField(max_length=100) # for Drink in form.py Drink = models.CharField(max_length=100) # for Smoke in form.py Smoke = models.CharField(max_length=100) # for Caffeine in form.py Caffeine = models.CharField(max_length=100) # for menopause in form.py menopause = models.CharField(max_length=100) # for periods in form.py periods = models.CharField(max_length=100)
30th May 2021, 7:20 PM
Benjamin
Benjamin - avatar
0
AKSHAY🇮🇳 this is in forms.py from django import forms class medical_reports(forms.ModelForm): # Healthy & Unhealthy Habits # Exercise Exercise_choice = [ ('Never'), ('1 - 2 days'), ('3 - 4 days'), ('5 + days'), ] Exercise = forms.ChoiceField(choices=Exercise_choice, widget=forms.RadioSelect) # Eating following a diet Eating_choices = [ ('I have a loose diet'), ('I have a strict diet'), ('I dont have a diet plan'), ] Eating = forms.ChoiceField(choices=Eating_choices, widget=forms.RadioSelect) # Alcohol Consumption Drink_choices = [ ('I dont drink'), ('1-2 glasses/day '), ('3-4 glasses/day'), ('5+ glasses/day'), ] Drink = forms.ChoiceField(choices=Drink_choices, widget=forms.RadioSelect) # Do you smoke? Smoke_choices = [ ('No'), ('0-1 pack/day'), ('1-2 packs/day'), ('2+ packs/day'), ] Smoke = forms.ChoiceField(choices=Smok
30th May 2021, 7:22 PM
Benjamin
Benjamin - avatar
0
I have tried it and am getting this errors
1st Jun 2021, 6:39 PM
Benjamin
Benjamin - avatar
0
AKSHAY🇮🇳 Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "C:\Users\user\anaconda3\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line utility.execute() File "C:\Users\user\anaconda3\lib\site-packages\django\core\management\__init__.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\user\anaconda3\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\user\anaconda3\lib\site-packages\django\core\management\base.py", line 369, in execute output = self.handle(*args, **options) File "C:\Users\user\anaconda3\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "C:\Users\user\anaconda3\lib\site-packages\django\core\management\commands\makemigratio
1st Jun 2021, 6:41 PM
Benjamin
Benjamin - avatar
0
AKSHAY🇮🇳 this is what I did
1st Jun 2021, 6:41 PM
Benjamin
Benjamin - avatar
0
AKSHAY🇮🇳 from django.db import models Exercise_choice = ( ('Never', 'never'), ('1 - 2 days', '1 - 2 Days'), ('3 - 4 days', '3 - 4 Days'), ('5 + days', '5 + Days'), ) class Exercises(models.Model): Exercise = models.CharField(choices=Exercise_choice, max_length=10, default='never')
1st Jun 2021, 6:42 PM
Benjamin
Benjamin - avatar
0
AKSHAY🇮🇳 from django import forms from ..models import Exercises class Exercise_choice(forms.ModelForm): class Meta: model = Exercises fields = "__all__" widgets = {'Exercise': forms.RadioSelect}
1st Jun 2021, 6:43 PM
Benjamin
Benjamin - avatar