django-modelforms

Django Imagefield not working properly via ModelForm

I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me. I'm using a ModelForm so I can expose a few fields from a model for editing. 2x ImageField, 1x TextField. The Form is processed and the TextField works. The two ImageFields do not work and ...

Substituting an absent field value when saving a modelform

I have a this model: class Fleet(models.Model): company = models.ForeignKey("Company", editable=False) aircraft = models.ForeignKey("Aircraft") size = models.IntegerField(default=1) description = models.TextField(blank=True) def __unicode__(self): return u"%s" % (self.aircraft, ) And then a form based on this model: class F...

raw_id_fields for modelforms

I have a modelform which has one field that is a ForeignKey value to a model which as 40,000 rows. The default modelform tries to create a select box with 40,000 options, which, to say the least is not ideal. Even more so when this modelform is used in a formset factory! In the admin, this is easiely avoidable by using "raw_id_fields", ...

Changing field type in a Django ModelFormset

In a Django ModelForm, you can change the widget type of a field like so: class EntryForm(ModelForm): entity = forms.CharField() class Meta: model = Entry I can easily create a modelformset from the same model like so: EntryFormSet = modelformset_factory(Entry) But is there a way to include the input field type ch...

Changing data in a django modelform

I get data in from POST and validate it via this standard snippet: entry_formset = EntryFormSet(request.POST, request.FILES, prefix='entries') if entry_formset.is_valid(): .... The EntryFormSet modelform overrides a foreign key field widget to present a text field. That way, the user can enter an existing key (suggested via an Aja...

Model Formset overwrites existing entries instead of adding

I have a Model Formset that should take files sent via POST and add them to the database. However, the form includes the IDs of files already in the database, which leads to information being overwritten. For example, the HTML output for the form looks like this: <label for="id_files-0-theFile">File:</label> <input type="file" nam...

django model/modelForm - How to get dynamic choices in choiceField?

hi i'm experimenting with django and the builtin admin interface. I basically want to have a field that is a drop down in the admin UI. The drop down choices should be all the directories available in a specified directory. If i define a field like this: test_folder_list = models.FilePathField(path=/some/file/path) it shows me all t...

django auto slug in model forms like prepopulated-fields in django admin.

is there a way to get the same results of using pre-populated fields in django's admin site for slug fields in a standard modelform ...

Django ModelForm Validate custom Autocomplete for M2M, instead of ugly Multi-Select

Given the following models (cut down for understanding): class Venue(models.Model): name = models.CharField(unique=True) 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) ven...

how to define a widget in a model attribute

Simply, I write: # forms.py class NoteForm(ModelForm): def __init__(self, *args, **kwargs): super(NoteForm, self).__init__(*args, **kwargs) #add attributes to html-field-tag: self.fields['content'].widget.attrs['rows'] = 3 self.fields['title'].widget.attrs['size'] = 20 class Meta: model = Note fields = ('title'...

How to validate/clean() a unique=True field without using a ModelForm?

In a custom Form, how does one validate a Model's field's uniqueness (i.e., has unique=True set)? I know that django's ModelForm automatically performs a validate_unique() function that is called within the BaseModelForm's clean() method -- so, when using ModelForm, this will be handled correctly (as it is in the Admin). However, I a...

Django Admin: Getting request in ModelForm's constructor

The below code removes certain values from a drop down menu. It works fine but I want to remove the value if the user lacks certain permissions. How can I access request.user in the ModelForm's constructor? Or is there a better way to accomplish what I am trying to do? class AnnouncementModelForm(forms.ModelForm): def __init__(sel...

How to check if a FileField has been modified in the Admin of Django ?

Hello, I am trying to do a model with a file that shouldn't be modified. But the comment of the file can be. Here is what I did, but we cannot modify the comment. How can I test if a new file (using the browse button) as been sent and in this case only, create a new instance of the model ? If no upload of a new file, update the comment...

What is wrong with my django's model field ?

I am trying to do a PhoneField that convert the value as a standardized value. In this case, I want to use this clean method. def clean(self): phone = self.cleaned_data.get('phone') # Is it already standardized ? if phone.startswith('+'): mo = re.search(r'^\+\d{2,3}\.\d{9,11}$', phone) if not mo: raise...

Django ModelForm: accessing a field's value in the view template

Q: Is it possible to pull out the value for one of my ModelForm's fields while in the template? If so, how? Background: I have a search form that I'm using to allow users to filter down query results. And, the table of results, along with the form can, be exported to an Excel sheet. When the page is rendered w/ the new results, the inpu...

How to create Django FormWizard for one Model?

I have Django Model with many fields which user must fill. If I'll create one ModelForm for this Model it will be big enough for one form. I want to split it using FormWizard. I think it's possible first to create forms dynamically and then create FormWizard using them. Is this good approach or is there any better way? ...

File does not upload from web form in Django

Howdy - I've written a very simple app to accept job applications including a resume upload. Running the bundled server for development locally, I can successfully upload files via the web form on the front end and the admin interface. Running it on the remote server (Apache with mod_python) I can successfully upload files via the admin...

How to create a UserProfile form in Django with first_name, last_name modifications ?

If think my question is pretty obvious and almost every developer working with UserProfile should be able to answer it. However, I could not find any help on the django documentation or in the Django Book. When you want to do a UserProfile form in with Django Forms, you'd like to modify the profile fields as well as some User field. B...

How do you make a Django Form for a model and all of its children following a particular foreign key?

For example, lets say you want to create a ModelForm for Supervisor that also allows you to create or update 3 or more Underlings in the same form. from django.db import models class Supervisor(models.Model): name = models.CharField(max_length=100) class Underling(models.Model): supervisor = models.ForeignKey(Superisor, related...

Django: validationerror problem when multiple modelforms are pointing to one model

I have a set of forms (lets say formA,formB,formC) on a page that collects data and exposes different fields depending on which form is used. These forms have different underlying processing logic (and client-side logic) but have the same model If formA is submitted,I noticed that a form.validationerror raised in formA's clean method th...