Django choicefield choices not displaying - django

Im using a choicefield and setting 2 values - 'students', 'teachers',
but for some reason when the form displays it only shows 'teachers' and not 'students'.
class SignUpShortForm(SignUpForm):
role = forms.ChoiceField(
choices=[],
widget=forms.Select(attrs={'class':'form-control'}),
label='I am a...',
)
self.fields['role'].choices = [('Teacher', 'Teacher2')]

Please look here You add to your choices only values without keys. Code might look like this:
CHOICES = (
('students', 'Students'),
('teachers', 'Teachers'),
)
class SignUpShortForm(SignUpForm):
role = forms.ChoiceField(
choices=CHOICES,
widget=forms.Select(attrs={'class':'form-control'}),
label='I am a...',
)

Related

Make a form that pulls choices from table in DB and then allows user to change to different foreign keys

So I have a user model with the following columns:
username = models.CharField(db_column='Username',max_length=32,unique=True)
email = models.CharField(db_column='Email',max_length=255)
password = models.CharField(db_column='Password',max_length=128)
prosthodontist = models.ForeignKey('Prosthodontist',on_delete=models.SET_NULL,null=True)
I'm trying to make a dropdown that allows the user to change their Prosthodontist value through django forms. It can't be a static list cause it has to always have every available Prosthodontist as they get added.
Just for show this is what I have so far along the lines of the form:
class ChangeProsthodontistForm(forms.ModelForm):
class Meta:
model = User
fields = ('prosthodontist',)
prosthodontist = forms.ChoiceField(
label = "Prosthodontist",
widget = forms.Select(
attrs={
'id':'prosthodontist',
},
),
choices=()
)
Please help me with this cause I'm really confused I feel like I could be able to iterate through the entries with a for loop but I feel like there has to be a better way through Django.
You answer is ModelChoiceField.
prosthodontist = forms.ModelChoiceField(
# ...
queryset = Prosthodontist.objects.all(),
# ...
)

How to order django queryset using the Choices displayed label?

Is it possible to order the query set by choices displayed label? If not, can you please advise on how to achieve this? Or is there a way to create a custom ordering?
Thanks in advance!
Choices
C_TYPES = Choices(
('ac_sm', 'ac_sm', _('Ta-Class - Small')),
('ac_md', 'ac_md', _('Ta-Class - Medium')),
('ac_lg', 'ac_lg', _('Ta-Class - Large')),
)
Model
class FactoryLeadType():
c_type = models.CharField(
_('Chiller Type'),
choices=C_TYPES ,
default=C_TYPES .ac_sm,
max_length=50,
)
class Meta:
ordering = ( `get_c_type_display()` )

Choices in Django forms

I am editing the code below to fit forms. What would be the most convenient replacement for choices (below) in a way that they would function similarly to their use in models?
class MyRegistrationForm(UserCreationForm):
email = forms.EmailField(max_length=30)
USER_LEVEL = (
('admin', 'Admin'),
('staff', 'Staff'),
('hof', 'Head of Facilities'),
('user', 'User'),
)
user_level = forms.CharField(max_length=7, choices=USER_LEVEL, default='user')
It sounds like you want to use a ChoiceField.
user_level = forms.ChoiceField(max_length=7, choices=USER_LEVEL, default='user')

django admin - custom fields radio - admin.HORIZONTAL not working

I have this custom field image_choice in django admin as a radio select.
IMG_CHOICES = (
('embed', _('Embed code')),
('file', _('Upload image')),
('link', _('Image Link'))
)
class BlogArticleForm(forms.ModelForm):
class Media:
js = ('js/myjs.js')
image_choice = forms.ChoiceField(choices=IMG_CHOICES, widget=forms.RadioSelect)
class BlogArticleAdmin(admin.ModelAdmin):
form = BlogArticleForm
fields = ['title', 'description', 'image_choice', 'image_embed', 'image_file', 'image_link']
admin.site.register(models.BlogArticle, BlogArticleAdmin)
I cannot get these radio buttons line up horizontally.
I tried:
radio_fields = {'image_choice': admin.HORIZONTAL}
and
radio_fields = {form.image_choice: admin.HORIZONTAL}
but I keep getting this error:
type object "BlogArticleForm" has no attribute 'image_choice'
how can I achieve this?
this is how it looks like right now:
Take a look at this line.
So I guess it should look something like this:
from django.contrib.admin.options import get_ul_class
class BlogArticleForm(forms.ModelForm):
image_choice = forms.ChoiceField(
choices=IMG_CHOICES,
widget=widgets.AdminRadioSelect(
attrs={'class': get_ul_class(admin.HORIZONTAL)}
))
UPD: I'm stupid :(
It says: if 'widget' not in kwargs: bla-bla-bla adds widget. So this should work with no widget:
image_choice = forms.ChoiceField(choices=IMG_CHOICES)
# And set
radio_fields = {'image_choice': admin.HORIZONTAL}

Django - Use Radio Buttons or Drop Down List for input

I made a model something like this:
class Enduser(models.Model):
user_type = models.CharField(max_length = 10)
Now I want user_type to have only one of the given values, say any one from ['master', 'experienced', 'noob']
Can I do this with Django?
Also, how can I display a list of radio buttons or drop-down list/select menu to chose one of these values?
You can take advantage of the choices attribute for CharField:
class Enduser(models.Model):
CHOICES = (
(u'1',u'master'),
(u'2',u'experienced'),
(u'3',u'noob'),
)
user_type = models.CharField(max_length = 2, choices=CHOICES)
This will save values 1,2 or 3 in the db and when retrieved the object, it will map it to master, experienced or noob. Take a look at the docs for more info.
Hope this helps!
Use model field choices:
CHOICES = (
('foo', 'Do bar?'),
...
)
class Enduser(models.Model):
user_type = models.CharField(max_length = 10, choices=CHOICES)