django-modelforms

How do I detect if a formset has any errors at all in a template?

Thanks to the fantastic inline model formsets in django I have a pretty advanced form with 4 inline formsets. In the template I display each formset in a tab. Everything works really slick but I would like to color a tab red if the formset in that tab has any validation errors at all. So I tried this: <div id="tabs"> <ul> <l...

ModelForm and error_css_class

Hi guys, my problem is simple. Where the right place for a custom error_css_class value is when using ModelForm? I tried this: class ToolForm(ModelForm): error_css_class = 'wrong_list' class Meta: model = Tool widgets = { 'name' : TextInput(attrs={'class': 'small_input corners'}), 'description' : T...

Boolean fields not saving in a model form

I have a form class that looks like this: class ApplicationDetailsForm(ModelForm): worked_in_industry = forms.TypedChoiceField(coerce=int, choices=((1, 'Yes'), (0, 'No')), widget=forms.RadioSelect()) class Meta: model = ApplicantDetails fields = ('work_experien...

Unable to override SelectMultiple widget in ModelForm (Django)

I have a ManyToManyField that I want to present in a form, as a CheckboxSelectMultiple widget. Why don't any of these methods work? (See Attempt #1, #2 and #3 below.) According to everything I've read in the docs and on SO, at least one of them should work. But I still have a stubborn SelectMultiple widget that refuses to budge. from d...

django ModelForm for subclassed models : Error for one, but not for another ('NoneType' object has no attribute 'label')

I'd like to ask for you guidance in the following matter in django: I am using the following models: class QItem(models.Model): isWhat = models.CharField(max_length=100, blank=True, choices=ISWHAT) slug = models.SlugField(blank=True) script = models.CharField(max_length=100) comment = models.TextField(blank=True, null=...

django modelform css class for select

Hi. I am trying to add in a class with the name of autocomplete into one of my select. class MyForm(ModelForm): class Meta: model = MyModel exclude = ['user'] def __init__(self, user, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['specie'].queryset = Specie.objects....

Form, Model, Model Form

So I can create all the necessary db fields in my models. I can create forms to access these models. And if I create models from forms: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/ I cant understand why I have to redefine the Fields like these: class AuthorForm(forms.Form): name = forms.CharField(max_length=100)...

How to use new Django 1.2 readonly_fields in ModelForm

I'm trying to use the new readonly_fields in a ModelForm. class TrainingAddForm(forms.ModelForm): class Meta: model = TrainingTasks readonly_fields = ('trainee_signed','trainee_signed_date') But this does not work. Am I missing something or is this not possible? ...

Django model form and objects database id

Hi I have a complex django object, which has properties of other class types. This gets like this: class Order: contractor - type Person some other fields.... In my form I'd like to be able to either choose existing Person object from the dropdown or add a new one with a form. I've managed to create forms and appropriate workfl...

Django / Python, Using Radio Button for boolean field in Modelform?

I'm trying to use a radio button in my modelform but its just outputting nothing when I do an override this way (it just prints the label in my form, not the radio buttosn, if I don't do the override it does a standard checkbox) My modelfield is defined as: Class Mymodelname (models.Model): fieldname = models.BooleanField(max_lengt...

How do I set default widget attributes for a Django ModelForm?

I'd like to set the class attribute for the TextInput Widget to one value for all the fields in my form that use it without having to list them all in Meta: widgets = {.... Is this possible? Thanks. ...

Django Model data incomplete

I have a model with 6 fields, but when I access them using the author field and print the result, it only displays 4 of them, field5 is not shown. The admin shows all fields. My view, model and modelform are below. if request.POST: c1 = Datastore.objects.get(author = request.user) return HttpResponse(c1) class Datastore(models...

Django ModelForm and datetimefield : no widget

I try to display a modelform containing a datetime field But when I try to display this field, anything wants to appear. In the model form, when I print self.fields : {'status': <django.forms.fields.TypedChoiceField object at 0x1340e10>, 'reserved': <django.forms.fields.BooleanField object at 0x1340cd0>, 'date_publication': None, 'dat...

Django Formset populating when it should be blank

My goal is to have a formset that lists 5 versions of the same form. On submit the form should create the filled out forms in the DB. My issue is that the forms come repopulated with data. Which is bad. Any thoughts on what I might be doing wrong? Models.py from django.db import models from django.contrib.auth.models import User PR...

Django ModelForm Ajax Upload

I'm using an Ajax code for uploading files. Django takes good care of file uploads on ModelForms. Just writing form.save() would upload any file data in the header, manage creating the folders if needed and even rename the file if a duplicate already exists. Take this ModelForm which only has one filed named file for example: class Uplo...

Django - populate a MultipleChoiceField via request.user.somechoice_set.all()

Is there a more efficient, or cleaner way to do the following? class SpamForm(ModelForm): some_choices = fields.MultipleChoiceField() def __init__(self, *args, **kwargs): super(SpamForm, self).__init__(*args, **kwargs) self.fields['some_choices'].choices = [[choice.pk, choice.description] for choice in self.inst...

Can you access a Django Model "property" from it's ModelForm?

I have a Django model class with a non-model-field property, ex: def _get(self): return "something" description = property(_get) I'm using the model class with in a ModelForm / ModelFormset. Is there any way to access the property from the form / formset? If not, what's best practice for including extra "display" data in a djang...

How can I set arbitrary attributes on Django Model Fields, then access them in a ModelForm?

What Django Does Django's Model Field "blank" attribute, and the way it gets negated and turned into the Form Field "required" attribute when you create a ModelForm, is pretty cool. It allows me to set the property on the model where it belongs, but have the "required" attribute available when handling a ModelForm created from it. Havin...

Want to restrict choices to a subset of rows

I'm working on a video management application where each video clip is associated with a single program-name and a single category-name, but programs and categories can be associated with multiple different videos. (This part is straight forward.) What's different is that the choices for category-names vary on a per program basis. For ...

How can I modify a widget's attributes in a ModelForm's __init__() method?

I want to programatically modify the widget attributes of a field in a Django ModelForm's init() method. Thus far, I've tried the following def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['my_checkbox'].widget_attrs(forms.CheckboxInput(attrs={'onclick':'return false;'})) Unfortun...