django-forms

Display some free text in between Django Form fields

Hi all, I have a form like the following: class MyForm(Form): #personal data firstname = CharField() lastname = CharField() #education data university = CharField() major = CharField() #foobar data foobar = ChoiceField() Since some fields (like foobar) are populated from the database i can't use another method othe...

Get Primary Key after Saving a ModelForm in Django

How do I get the primary key after saving a ModelForm? After the form has been validated and saved, I would like to redirect the user to the contact_details view which requires the primary key of the contact. def contact_create(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(...

Iterate over choices in CheckboxSelectMultiple

I have a CheckboxSelectMultiple field, why can't I iterate over the single choices? This doesn't work: {%for choice in form.travels.choices%} {{choice}} {%endfor%} Even specifying {{choice.0}} doesn't help, how could i do this? Thanks ...

How to Modify Choices of ModelMultipleChoiceField

Let's say I have some contrived models: class Author(Model): name = CharField() class Book(Model): title = CharField() author = ForeignKey(Author) And let's say I want to use a ModelForm for Book: class BookForm(ModelForm): class Meta: model = Book Simple so far. But let's also say that I have a ton of ...

Customize/remove Django select box blank option

I'm using Django 1.0.2. I've written a ModelForm backed by a Model. This model has a ForeignKey where blank=False. When Django generates HTML for this form it creates a select box with one option for each row in the table referenced by the ForeignKey. It also creates an option at the top of the list that has no value and displays as ...

form with CheckboxSelectMultiple doesn't validate

I have a form with a choice field that is using CheckboxSelectMultiple widget: foo = forms.ChoiceField(widget=forms.CheckboxSelectMultiple, choices=( ("1", "ONE"), ("2", "TWO"), )) The form renders fine showing ...

Django: How to use stored model instances as form choices?

I have a model which is essentially just a string (django.db.models.CharField). There will only be several instances of this model stored. How could I use those values as choices in a form? To illustrate, the model could be BlogTopic. I'd like to offer users the ability to choose one or several topics to subscribe to. I started writing...

Different ManyToManyField widget

The default django 1.0.2 ManyToManyField widget (a multi-select) is difficult to use when there are a lot of things in the select box. Is there another widget available that gives a comma separated list of id's in a textarea? If this is not available what do I need to do to write one, and have it show up on ModelForm.as_p() and in the...

How to organize checkboxes in several columns in Django forms

I am a newbie to Django and web-development in general so be patient for maybe a very dumb question :) I have a form generated from a model and in this form I have about 20 checkboxes. Now they are aligned in one long column and it looks not very nice from a UI point of view. I'd like this column to be split in several ones but still ha...

Django Form values without HTML escape.

Hi, I need to set the Django forms.ChoiceField to display the currency symbols. Since django forms escape all the HTML ASCII characters, I can't get the &#36; ( ) or the &pound; ( ) to display the currency symbol. <select id="id_currency" name="currency"> <option value="&amp;#36;">&#36;</option> <option value="&amp;pound;">&...

In Django, how can I pre-populate an edit form from a model/child, but save submission as new instance?

I'm trying to prepopulate a ModelForm and an inlineformset_factory with an instance of a Model, BUT when the user submits the form, I need to create a new instance of the Model and it's related child records. Example Model: class Artist(models.Model): artist = models.CharField(max_length=100) class Song(models.Model): artist =...

How to store data in Django Model without having an input field

I have model for example like this: class Meeting(models.Model): date = models.DateTimeField(default=datetime.datetime.now) team = models.CharField(max_length=100) class Meta: verbose_name_plural = u'Meetings' ordering = ['-date', 'team'] def __unicode__(self): return u'%s %s' % (self.date, self...

Overriding the save method in Django ModelForm

I'm having trouble overriding a modelform save method. This is the error I'm receiving: Exception Type: TypeError Exception Value: save() got an unexpected keyword argument 'commit' My intentions are to have a form submit many values for 3 fields, to then create an object for each combination of those fields, and to save each of th...

ModelForms in Django where the underlying model depends on another model (via OneToOneField)

I have two models in my Django application, for the purposes of storing search parameters used for some homology search programs: # models.py class Search(models.Model): """A class to represent search runs.""" program = models.CharField(max_length=20) results_file = models.FileField( upload_to=(SEARCH_RESULTS_DIR) ...

ModelMultipleChoiceField validation acting differently depending on widget

I have a form with a ModelMultipleChoiceField and it works like a charm. However, when I set widget=forms.RadioSelect the form validation always fails with the error message "Enter a list of values." As I said, it works perfectly well with the default widget, but not with the radio select widget. The POST request to validate the form ...

Django validation array of errors

I am using rf['email'].errors As said in docs, I can use it to have array of errors. [str(e) for e in rf['email'].errors] #give me ["<django.utils.functional.__proxy__>"] If repr or str - it gives ul's or string of array. So it worked only when I used repr and eval together. But I think its stupid solution. eval(`rf['email'].e...

Django BooleanField as radio buttons?

Is there a widget in Django 1.0.2 to render a models.BooleanField as two radio buttons instead of a checkbox? ...

How can I build multiple submit buttons django form?

I have form with one input for email and two submit buttons to subscribe and unsubscribe from newsletter: <form action="" method="post"> {{ form_newsletter }} <input type="submit" name="newsletter_sub" value="Subscribe" /> <input type="submit" name="newsletter_unsub" value="Unsubscribe" /> </form> I have also class form: class Newsle...

How to check value transition in Django (django-admin) ?

I have a status field which has 3 values: pending, activated and rejected. If I am changing the value of status I want to have a check that activated cannot be changed to pending. I do not want to write stored-procs for this. Can I have the previous value in Django before saving? Means new and old value. ...

Inline Form Validation in Django

Newbie request that seems difficult to implement. I would like to make an entire inline formset within an admin change form compulsory.. so in my current scenario when I hit save on an Invoice form (in Admin) the inline Order form is blank. I'd like to stop people creating invoices with no orders associated. Anyone know an easy way to...