modelform

django forms from two tables referencial integrity

i have a class named cv,and a class named university, and each user that completes his cv, should choose a University he studyes at. My problem is: one student can study at one or 2 or three universities, or may be a user that is not student. I need to take this data into a form, and i use ModelForm. The data from the Cv class, and fr...

How do I create a Django ModelForm, so that it's fields are sometimes required, sometimes not?

Ok, here is the question. Imagine I have a ModelForm which have only two fields. like this one: class ColorForm(forms.Form): color_by_name = forms.CharField() color = forms.IntegerField(widget = forms.Select(choices=COLOR_CHOICES)) So a user can either input a color name, a choose it from a list. Color is required, but that ...

get_model equivalent for ModelForms?

I have a multiple ModelForm classes that each represent a different Model. I would like to have a generic 'create' function that loads the specified model form based on a URL parameter. It is possible to load a model dynamically with this: model_name = 'TestModel' m = get_model('AppLabel', model_name) Does anyone know how I can achiev...

Set Django ModelForm visible fields at runtime?

I have a Django model: class Customer(models.Model): first_name=models.CharField(max_length=20,null=True, blank=True) last_name=models.CharField(max_length=25,null=True, blank=True) address=models.CharField(max_length=60,null=True, blank=True) address2=models.CharField(max_length=60,null=True, blank=True) city=models.CharField...

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 =...

Django: Edit multiple rows

Hi, I want a list of forms with permissions, and one save button.. Like this: http://www.freeimagehosting.net/image.php?c701b302fd.png But I can't get it working.. Some ideas? I try to do somethis like this: forms.py class ProjectPermission(ModelForm): class Meta: model = ObjectPermission exclude = ('content_type', 'obje...

Python/Django BooleanField model with RadioSelect form default to empty

Hello all, I'm using a Django ModelForm where my model contains a BooleanField and the form widget associated with that BooleanField is a RadioSelect widget. I'd like the the RadioSelect widget that renders to have no options selected so the user has to explicitly make a choice, but the form validation to fail if they make no selection...

Django "Duplicate" ModelForm

Hi I'm wondering if there is a simple way of creating a "duplicate" ModelForm in Django - i.e. a form that is prefilled with the content of an existing model instance (excepting certain fields, such as those that are unique), but creates a new instance when saved. I was thinking along the lines of supplying an instance to a ModelForm s...

How to access request in ModelForm for adding request.user as foreign key

I am trying to override save in the modelform to add the current user as the owner of a vehicle. But I am receiving 'NoneType' object has no attribute 'user' What am I forgetting? forms.py: class VehicleForm(ModelForm): class Meta: model = Vehicle exclude = ('slug', 'owner', ) def __init__(self, *args, **kwargs...

Django: MultipleChoiceField in admin to carry over previously saved values

I am having troubles to carry over previously selected items in a ModelForm in the admin. I want to use the forms.CheckboxSelectMultiple widget since that is the most straightforward UI in this usecase. It works as far that when saving, the values are stored. But when editing the previously saved item, the values previously saved in this...

Django using a newly create object in reverse redirect

I am trying to pull the id from the newly created project object so I can redirect the user to the page containing the new project. Right now I get "'ProjectAddForm' object has no attribute 'id'". I have read online that this should work but for some reason it's not. if request.method == 'POST': form = ProjectAddForm(request.P...

Django ModelForm fails validation with no errors

Ok, I have been staring at this for hours trying to figure out what's going on, to no avail. I am trying to create a ModelForm using the 'instance' keyword to pass it an existing model instance and then save it. Here is the ModelForm (stripped considerably from the original in my attempts to identify the cause of this problem): class ...

Django ModelForms - 'instance' not working as expected

I have a modelform that will either create a new model or edit an existing one - this is simple and should work, but for some reason I'm getting a new instance every time. The scenario is this is the first step in an ecommerce order. The user must fill out some info describing the order (which is stored in the model). I create the model...

Localization of Django application only applies to forms.py and not to models.py

I have a problem when trying to localize my application. It is available in two languages: english and german. The problem appears when the browser has the language set english(United States) and in my settings file is set to 'de' and vice-versa. Some fields appear in english, others in german. My model contains CharField, DecimalField a...