django-forms

How can I test a form's validation logic in a unit test driver in Django?

I want to test the is_valid portion of a form's validation logic. In my test driver I have: test_animal = Animal(name="cat", number_paws="4") test_animal_form = AnimalForm(instance=test_animal) assertEqual(test_animal_form.is_valid(), True) The assertion fails, but from what I see there shouldn't be any errors in the form. I...

Django modelform: is inline adding related model possible?

I really hope this is not a duplicate: I couldn't find anything but that could just mean I'm not good at searching :) I have a Django app, and the staff is already using the admin app for... well, administration purposes. I also created a quick data entry page for a specific model, created substantially by dropping a modelform inside th...

Django: setting required=True on formfield when running clean() .. actually just before

Hi all, I'm trying to force a form field to be required based on a choice widget during validation. def clean(self): cleaned_data = self.cleaned_data if cleaned_data.get('periodical') == True: if cleaned_data.get('period_start_date') == None: msg = _('custom message') self._errors['period_start_d...

How to make a multi choice form field on app engine

Hi, I'm building an application on app Engine and I want to make a form field with multiple choices. Here is my form (it uses django.newforms from the app engine sdk (django 0.96)) : from google.appengine.ext.db import djangoforms from django import newforms class KeywordForm(djangoforms.ModelForm): class Meta: model = Key...

Django ModelForm for Many-to-Many fields

Consider the following models and form: class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: model = Topping When you view the Toppi...

Django localflavor for models? Default widget?

I see these nice "local flavors" for Canada, but they're only form fields. How would I use them in my model? I can create them all as CharFields sure, but then is there a way to set the default form widget from inside the model so that when I create a ModelForm it'll use them? ...

Why is my custom Widget class returning a string instead of a datetime.time?

I have a custom TimeSelector Django form widget that aggregates three select widgets into a single time value. Here's its value_from_datadict method: def value_from_datadict(self, data, files, name): hour = int(data.get(self.hour_field % name)) minute = int(data.get(self.minute_field % name)) am_pm = data.get(self.am_pm_fiel...

django choiceField with CheckboxSelectMultiple: all selected by default?

Hi, I'm using a choiceField with the CheckboxSelectMultiple widget. Is it possible to render all checkboxes as checked by default? Thanks! ...

building dynamic forms in django

I'm trying to build a form dynamically based on the field and its definitions stored in a database. In my db, I have defined 1 checkbox with some label and 1 textfield with some label. How do I build a form dynamically in my view from the data in the db? Thanks ...

adding errors to Django form errors.__all__

How do I add errors to the top of a form after I cleaned the data? I have an object that needs to make a REST call to an external app (google maps) as a pre-save condition, and this can fail, which means I need my users to correct the data in the form. So I clean the data and then try to save and add to the form errors if the save doesn'...

passing arguments to a dynamic form in django

I have a Dynamic Form in forms. How can I pass a argument from my view when I instantiate my form. something like form = DynamicForm("some string argument") This is the form I have: class DynamicForm(Form): def __init__(self, *args, **kwargs): super(DynamicForm, self).__init__(*args, **kwargs) for item in range(5): ...

How to access request.user from an admin ModelForm clean method?

I'm doing some stuff on 'clean' on an admin ModelForm: class MyAdminForm(forms.ModelForm): def clean(self): # Some stuff happens... request.user.message_set.create(message="Some stuff happened") class MyAdmin(admin.ModelAdmin): form = MyAdminForm Other than the threadlocals hack - how do I access request.user ...

Django ChoicesField divider?

I'm using a ChoicesField in my form, but I want to put a divider in it like this: COUNTRIES = ( ('CA', _('Canada')), ('US', _('United States')), (None, _('---')), # <---------- ('AF', _('Afghanistan')), ('AX', _('Aland Islands')), ('AL', _('Albania')), ('DZ', _('Algeria')), ('AS', _('American Samoa')), ...

Django models with OneToOne relationships?

Let's say I'm using the default auth.models.User plus my custom Profile and Address models which look like this: class Profile(models.Model): user = models.OneToOneField(User) primary_phone = models.CharField(max_length=20) address = models.ForeignKey(Address) class Address(models.Model): country = CountryField(default=...

Customizing Django Form: Required and InputId?

I'm trying to customize how my form is displayed by using a form_snippet as suggested in the docs. Here's what I've come up with so far: {% for field in form %} <tr> <th><label for="{{ field.html_name }}">{{ field.label }}:</label></th> <td> {{ field }} {% if field.help_text %}<br/><small class="help_text">{{ fie...

Django: One FormWizard for multiple models

Hi all, Would it be possible/best practice to use 1 FormWizard for manipulating multiple models? I've experimented with the FormWizard and have defined all the forms. The page 'flow' itself works like a charm. However with all the checks that need to be done and Models that are manipulated it feels like I'm sticking code in the form's...

Django having trouble linking a UserProfile to User

Hi All, So im wanting to extend the django auth.User model to add extra fields. Ive read a few articles here and there, and am almost there. Im just trying to make the signup form at the moment. So a new user can sign up. I can get the new User created but when I come to save the UserProfile object to the database it fails citing Cann...

Django unit testing for form edit

Someone has probably already developed a technique for relieving the tedium for the following idiomatic unit test: GET a url with form data already populated POST a revised form with one or more fields edited Check response (profit!) Step 2 is the most tedious, cycling through the form fields. Are there any time-saving hacks for tes...

Paginating the results of a Django forms POST request

I'm using Django Forms to do a filtered/faceted search via POST, and I would like to Django's paginator class to organize the results. How do I preserve the original request when passing the client between the various pages? In other words, it seems that I lose the POST data as soon as I pass the GET request for another page back to my...

Django-contact-forms: how to subclass and supply 'initial' parameter?

I have a Django form that is subclassed from the django-contact-form application. I want to supply an initial parameter to the form (which will vary depending on context). This code returns the contact form fine, but obviously doesn't supply an initial parameter, so I need to extend it: def contact_form(request): scraper_form = scrape...