Questions tagged [modelform]

Questions related to the usage of a Django helper class ModelForm that let programmers create a Form class from a model.

688 questions
124
votes
8 answers

Django ModelForm: What is save(commit=False) used for?

Why would I ever use save(commit=False) instead of just creating a form object from the ModelForm subclass and running is_valid() to validate both the form and model? In other words, what is save(commit=False) for? Can you provide hypothetical…
sgarza62
  • 5,998
  • 8
  • 49
  • 69
80
votes
10 answers

Django's ModelForm unique_together validation

I have a Django model that looks like this. class Solution(models.Model): ''' Represents a solution to a specific problem. ''' name = models.CharField(max_length=50) problem = models.ForeignKey(Problem) description =…
sttwister
  • 2,279
  • 1
  • 19
  • 23
56
votes
5 answers

Django: Make certain fields in a ModelForm required=False

How do I make certain fields in a ModelForm required=False? If I have: class ThatForm(ModelForm): class Meta: widgets = {"text": Textarea(required=False)} Or if I have: class ThatForm(ModelForm): text = Textarea(required=False) Django…
Synthead
  • 2,162
  • 5
  • 22
  • 25
49
votes
3 answers

Creating a model and related models with Inline formsets

[I have posted this at the Django users | Google Groups also.] Using the example in the inline formset docs, I am able to edit objects belonging a particular model (using modelforms). I have been trying to follow the same pattern for creating new…
chefsmart
  • 6,873
  • 9
  • 42
  • 47
45
votes
4 answers

how can I change the modelform label and give it a custom name

I want to create a custom name for on of the labels in my modelform this is my forms.py class PostForm(forms.ModelForm): body = forms.CharField(widget=PagedownWidget) publish = forms.DateField( widget=forms.SelectDateWidget, …
nothingness
  • 971
  • 3
  • 10
  • 18
40
votes
10 answers

Django ModelForm has no model class specified

I am trying to use ModelForm: from django.db import models from django.forms import ModelForm class Car(models.Model): carnumber = models.CharField(max_length=5) def __unicode__(self): return self.carnumber class…
dpbklyn
  • 781
  • 3
  • 10
  • 19
32
votes
3 answers

Django ModelForm instance with custom queryset for a specific field

I have a model not unlike the following: class Bike(models.Model): made_at = models.ForeignKey(Factory) added_on = models.DateField(auto_add_now=True) All users may work at a number of factories and therefore their user profiles all have a…
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
23
votes
4 answers

Dynamically update ModelForm's Meta class

I am hoping to dynamically update a ModelForm's inline Meta class from my view. Although this code seems to update the exclude list in the Meta class, the output from as_p(), as_ul(), etc does not reflect the updated Meta exclude. I assume then that…
ashchristopher
  • 25,143
  • 18
  • 48
  • 49
23
votes
2 answers

Django ModelForm Imagefield Upload

I am pretty new to Django and I met a problem in handling image upload using ModelForm. My model is as following: class Project(models.Model): name = models.CharField(max_length=100) description = models.CharField(max_length=2000) …
Wei Xu
  • 1,629
  • 2
  • 19
  • 31
22
votes
1 answer

modelform: override clean method

I have two questions concerning the clean method on a modelform. Here is my example: class AddProfileForm(ModelForm): ... password = forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'class':'form2'})) …
rom
  • 3,592
  • 7
  • 41
  • 71
18
votes
1 answer

add extra field to ModelForm

I am adding an extra field to a Django ModelForm like that: class form(forms.ModelForm): extra_field = forms.CharField(label='Name of Institution') class Meta: model = db_institutionInstitution fields =…
sennierer
  • 213
  • 1
  • 3
  • 6
17
votes
4 answers

field choices() as queryset?

I need to make a form, which have 1 select and 1 text input. Select must be taken from database. model looks like this: class Province(models.Model): name = models.CharField(max_length=30) slug = models.SlugField(max_length=30) def…
robos85
  • 2,484
  • 5
  • 32
  • 36
16
votes
4 answers

How do I update an already existing row when using ModelForms?

I have a question on how to update an existing row in my database when one of the fields is my primary key. I am using ModelForm and Django-Piston - my main goal here is to have RESTful Post send to my webservice. I am able to have initial Posts be…
letsgofast
  • 257
  • 1
  • 3
  • 7
16
votes
2 answers

How to get Interdependent dropdowns in django using Modelform and jquery?

I am new to django and jquery. I am working on a django-based app where I have 3 drop downs in a form. 1. Campus 2. School 3. Centre The hierarchy is Campuses have schools and schools have centres. I want to interlink these drop-downs. For…
escapee
  • 413
  • 1
  • 6
  • 14
14
votes
6 answers

Django: how to hide/overwrite default label with ModelForm?

i have the following, but why does this not hide the label for book comment? I get the error 'textfield' is not defined: from django.db import models from django.forms import ModelForm, Textarea class Booklog(models.Model): Author =…
thedeepfield
  • 6,138
  • 25
  • 72
  • 107
1
2 3
45 46