django-modelforms

Replace textfields with dropdown select fields

I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20, blank=False) manufacturer = models.ForeignKey(Manufacturer) date_added = models.DateField(default=datetime.today) def __unicode__(self): name = ''+str(self.manufacturer)+" "+str(self.model) return nam...

How to cutomize a modelform widget in Django 1.1?

I'm trying to modify a Django form to use a textarea instead of a normal input for the "address" field in my house form. The docs seem to imply this changed from Django 1.1 (which I'm using) to 1.2. But neither approach is working for me. Here's what I've tried: class HouseForm(forms.ModelForm): address = forms.Textarea() # Should...

Django - How best to handle ValidationErrors after form.save(commit=False)

This is a fragment of my code from a view: if form.is_valid(): instance = form.save(commit=False) try: instance.account = request.account instance.full_clean() except ValidationError, e: # Do something with the errors here... I certainly don't want to do this 180 times. T...

Django: one form for two models (solved)

UPDATE The issue is solved, all the code you can see works. Hello! I have a ForeignKey relationship between TextPage and Paragraph and my goal is to make front-end TextPage creating/editing form as if it was in ModelAdmin with 'inlines': several fields for the TextPage and then a couple of Paragraph instances stacked inline. The proble...

Django admin: Add a "remove file" field for Image- or FileFields

I was hunting around the Internet for a way to easily allow users to blank out imagefield/filefields they have set in the admin. I found this: http://www.djangosnippets.org/snippets/894/. What was really interesting to me here was the code posted in the comment by rfugger: remove_the_file = forms.BooleanField(required=False) def save...

django - How to cross check ModelAdmin and its inlines?

I have two models (ModelParent and ModelChild) with same m2m fields on Subject model. ModelChild has a foreign key on ModelParent and ModelChild is defined as inline for ModelParent on admin page. ### models.py ### class Subject(Models.Model): pass class ModelParent(models.Model): subjects_parent = ManyToManyField(Subject)...

Form for Profile models ?

Is there a way to create a form from profile models ? For example... If I have this model as profile: class blogger(models.Model): user = models.ForeignKey(User, unique=True) born = models.DateTimeField('born') gender = models.CharField(max_length=1, choices=gender ) about = models.TextField(_('about'), null=True, ...

Validating an Autocomplete field in Django

I have models similar to the following: class Band(models.Model): name = models.CharField(unique=True) class Event(models.Model): name = models.CharField(max_length=50, unique=True) bands = models.ManyToManyField(Band) and essentially I want to use the validation capability offered by a ModelForm that already...

Using a custom form in a modelformset factory?

I'd like to be able to use a customized form in a modelformset_factory. For example: models.py class Author(models.Model): name = models.CharField() address = models.CharField() class AuthorForm(ModelForm): class Meta: model = Author views.py def test_render(request): myModelFormset = modelformset_factory(Aut...

How to customize a many-to-many inline model in django admin

I'm using the admin interface to view invoices and products. To make things easy, I've set the products as inline to invoices, so I will see the related products in the invoice's form. As you can see I'm using a many-to-many relationship. In models.py: class Product(models.Model): name = models.TextField() price = models.Deci...

why are read only form fields in django a bad idea?

I've been looking for a way to create a read-only form field and every article I've found on the subject comes with a statement that "this is a bad idea". Now for an individual form, I can understand that there are other ways to solve the problem, but using a read only form field in a modelformset seems like a completely natural idea. ...

Formatting inline many-to-many related models presented in django admin

I've got two django models (simplified): class Product(models.Model): name = models.TextField() price = models.IntegerField() class Invoice(models.Model): company = models.TextField() customer = models.TextField() products = models.ManyToManyField(Product) I would like to see the relevant products as a nice tabl...

Possible form field types per model field type

Django's documentation specifies for each model field type the corresponding default form field type. Alas, I couldn't find in the documentation, or anywhere else, what form field types are possible per model field type. Not all combinations are possible, right? Same question for widgets... ...

How do I specify an order of values in drop-down list in a Django ModelForm?

Ok, here is the question. class UserForm(forms.ModelForm): class Meta: model = User fields = ('team', 'related_projects') In models.py class User is defined as follows: class User (models.Model): team = models.ForeignKey(Team, verbose_name = _('team')) related_projects = models.ManyToManyField(Projec...

Django TextField max_length validation for ModelForm

Hi, Django does not respect the max_length attribute of TextField model field while validating a ModelForm. So I define a LimitedTextField inherited from the models.TextField and added validation bits similar to models.CharField: from django.core import validators class LimitedTextField(models.TextField): def __init__(self, *arg...

Django - How can I access model field validators from within form template?

Hi, In my generic form template, if a CharField has a max_length attribute, I display that beside the field. Form template: {% if field.field.max_length %} Max. {{ field.field.max_length }} char. {% endif %} I want to do this for TextFields, where max_length is not used, defined like that in the model: class Entry(models.Model)...

Django ModelFormSet: How to post-process edited and new records differently

I am trying to take a modelformset, then process the records that have been changed after the form is submitted. For new records, I need to do certain action like set one form field equal to a hidden field and add some time stamps. For edited records, I do not need to do these actions, just to save the model's edited data. For unchaged r...

How can I exclude a declared field in ModelForm in form's subclass?

In Django, I am trying to derive (subclass) a new form from ModelForm form where I would like to remove some fields (or to have only some fields, to be more correct). Of course obvious way would be to do (base form is from django.contrib.auth.forms): class MyUserChangeForm(UserChangeForm): class Meta(UserChangeForm.Meta): fields =...

Create an instance that represents the average of multiple instances

I have a Review Model like the one defined below (I removed a bunch of the fields in REVIEW_FIELDS). I want to find the average of a subset of the attributes and populate a ModelForm with the computed information. REVIEW_FIELDS = ['noise'] class Review(models.Model): notes = models.TextField(null=True, blank=True) CHOICES = ((1, ...

modelform fails is_valid w/o setting form.errors

I'm using a modelform to display only a subset of the fields in the model. When the form is submitted it fails form.is_valid() but form.errors is empty. I'd rather not display all my code here, but below is a sample: Model and Form class Videofiles(models.Model): active = models.CharField(max_length=9) filenamebase = models.Cha...