choicefield

How to use a choiceField declared in the model, in a form. django

柔情痞子 提交于 2019-12-05 23:39:37
I have this in my model.py class marca(models.Model): marcas = ( ('chevrolet', 'Chevrolet'), ('mazda', 'Mazda'), ('nissan', 'Nissan'), ('toyota', 'Toyota'), ('mitsubishi', 'Mitsubishi'), ) marca = models.CharField(max_length=2, choices= marcas) def __unicode__(self): return self.marca And I need to use it in my form.py I tried this but it doesn't work. class addVehiculoForm(forms.Form): placa = forms.CharField(widget = forms.TextInput()) tipo = forms.CharField(max_length=2, widget=forms.Select(choices= tipos_vehiculo)) marca = forms.CharField(max_length=2, widget=forms.Select(choices= marcas))

Django ModelChoiceField using distinct values from one model attribute

99封情书 提交于 2019-12-05 19:17:32
so I'm working on a django application where I have a model Event. Each Event has some attributes, say one of them is "hostname" (which I will use throughout as an example). I need to implement search functionality where a user can search for all events that has hostname == some_value, e.g. hostname == "myhost.foo.bar". Now, I wanted to allow the user to select among the valid options (i.e. the hostnames that actually exist in one or more event) in a combobox in the search form, so I use ModelChoiceFields for my form. My subclass of ModelChoiceView, for displaying the correct label: class

Django populate a form.ChoiceField field from a queryset and relate the choice back to the model object

送分小仙女□ 提交于 2019-12-05 02:05:24
问题 I have a simple form: class SubmissionQuickReplyForm(forms.Form): comment_text = forms.CharField(label='', required=False, widget=forms.Textarea(attrs={'rows':2})) I want to add a form.ChoiceField to the form, where the options in the ChoiceField is populated from a queryset. class SubmissionQuickReplyForm(forms.Form): comment_text = forms.CharField(label='', required=False, widget=forms.Textarea(attrs={'rows':2})) choice = forms.ChoiceField(...) For example, if I have: q = MyChoices.Objects

Django populate a form.ChoiceField field from a queryset and relate the choice back to the model object

风格不统一 提交于 2019-12-03 16:33:51
I have a simple form: class SubmissionQuickReplyForm(forms.Form): comment_text = forms.CharField(label='', required=False, widget=forms.Textarea(attrs={'rows':2})) I want to add a form.ChoiceField to the form, where the options in the ChoiceField is populated from a queryset. class SubmissionQuickReplyForm(forms.Form): comment_text = forms.CharField(label='', required=False, widget=forms.Textarea(attrs={'rows':2})) choice = forms.ChoiceField(...) For example, if I have: q = MyChoices.Objects.all() How can I populate the ChoiceField with the contents of q, so that when I am handling the results

choice property in google app engine

≡放荡痞女 提交于 2019-12-01 15:10:46
platform: django 1.0, google app engine, app-engine-patch , python 2.5.4 i am tring to use the choices attribute as i always have been using in django website STATUS_CHOICES = ( (1, _('Yet To Start')), (2, _('Running')), (3, _('Paused')), (4, _('Completed')), (5, _('Cancelled')), (6, _('Error')),) class Campaign(db.Model): name = db.TextProperty() status = db.IntegerProperty(choices=STATUS_CHOICES,default=2) now as i have moved to app-engine i dont see this is working... i am getting a BadValueError Property status is 3; must be one of ((1, u'Yet To Start'), (2, u'Running'), (3, u'Paused'), (4

Django ChoiceField populated from database values

纵然是瞬间 提交于 2019-11-30 14:42:07
问题 I am having problems using a ChoiceField to create a drop down list of values in the database. Here is the snippet of code from django import forms from testplatform.models import ServiceOffering class ContactForm(forms.Form): subject = forms.ChoiceField(queryset=ServiceOffering.objects.all()) #subject = forms.ModelMultipleChoiceField(queryset=ServiceOffering.objects.all()) The #subject.... line works, but when I use the line ChoiceField(queryset....) I get the following error. __init__() got

Django ChoiceField populated from database values

南楼画角 提交于 2019-11-30 11:21:00
I am having problems using a ChoiceField to create a drop down list of values in the database. Here is the snippet of code from django import forms from testplatform.models import ServiceOffering class ContactForm(forms.Form): subject = forms.ChoiceField(queryset=ServiceOffering.objects.all()) #subject = forms.ModelMultipleChoiceField(queryset=ServiceOffering.objects.all()) The #subject.... line works, but when I use the line ChoiceField(queryset....) I get the following error. __init__() got an unexpected keyword argument 'queryset' Any ideas? ChoiceField doesn't have a queryset. You're

How to get the label of a choice in a Django forms ChoiceField?

匆匆过客 提交于 2019-11-29 19:25:41
I have a ChoiceField, now how do I get the "label" when I need it? class ContactForm(forms.Form): reason = forms.ChoiceField(choices=[("feature", "A feature"), ("order", "An order")], widget=forms.RadioSelect) form.cleaned_data["reason"] would only give me "feature" or "order" or so. This may help: reason = form.cleaned_data['reason'] reason = dict(form.fields['reason'].choices)[reason] See the docs on Model.get_FOO_display() . So, should be something like : ContactForm.get_reason_display() In a template, use like this: {{ OBJNAME.get_FIELDNAME_display }} Kumar This the easiest way to do this:

Translate select options in Symfony2 class forms

五迷三道 提交于 2019-11-28 05:49:25
I'm using a class form in Symfony2 Beta3 as follows: namespace Partners\FrontendBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; class ConfigForm extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder->add('no_containers', 'choice', array('choices' => array(1 => 'yes', 0 => 'no'))); ... I want to translate the 'yes' and 'no' options, but I don't know how to use the translator here. You can use the translation resources as usual. This worked for me: $builder->add('sex', 'choice', array( 'choices' => array( 1

Django ChoiceField

风格不统一 提交于 2019-11-27 18:21:21
I'm trying to solve following issue: I have a web page that can see only moderators. Fields displayed on this page (after user have registered): Username, First name+Last name, Email, Status, Relevance, etc. I need to display table with information of all users stored in db with this fields, but two of fields have choices, so I want to make option that moderators can choose another option and after clicking on "Update" button this fields will be updated for selected user. I can to display all choices of "status" and "relevance" fields, and after I choose new options from dropdown db is updated