modelform

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 the html is generated when the ModelForm is created not when the as_*() is called. Is th...

django update modelform

I have a model as follows class UserPrivacy(models.Model): user = models.ForeignKey(User) profile = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) contact = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) friends = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) location = models.SmallI...

splitting a ManyToManyField over multiple form fields in a ModelForm

So I have a model with a ManyToManyField called tournaments. I have a ModelForm with two tournament fields: pay_tourns = forms.ModelMultipleChoiceField( queryset=Tourn.objects.all().active().pay_tourns(), widget=forms.CheckboxSelectMultiple()) rep_tourns = forms.ModelMultipleChoiceField( ...

Save modelForm to update existing record

I create a modelForm with instance to existing model (Book). I am not able to update the Books record. Adding a new record is fine but when I attempt to update, it appears to be unable to find the publisher (which is a foreign key). Error is "No Publisher matches the given query." models.py class Publisher(models.Model): name = models...

Model formsets and Date fields.

I have a model formset on a model with a couple of date fields - which again is a DateTimeField in the model. But when it displays in the template, it is shown as a text field. How can I show it as a drop down box ? WFormSet = inlineformset_factory(UserProfile, W, can_delete=False,exclude=[<list of fields to be excluded>], extra=3) T...

Creating modelformset from a modelform

Hi everyone, I have a model MyModel which contains a PK - locid, that is an AutoField. I want to construct a model formset from this, with some caveats: The queryset for the formset should be a custom one (say, order_by('field')) rather than all() Since locid for MyModel is an AutoField and thus hidden by default, I want to be able t...

Strange behavior with ModelForm and saving

This problem is very strange and I'm hoping someone can help me. For the sake of argument, I have a Author model with ForeignKey relationship to the Book model. When I display an author, I would like to have a ChoiceField that ONLY displays the books associated with that author. As such, I override the AuthorForm.init() method and I c...

Creating a model and related models with Inline formsets

[I have posted this at the Django users | Google Groups also.] Using the example at http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets, I am able to edit objects belonging a particular model (using modelforms). I have been trying to follow the same pattern for creating new objects using inline formsets, but ha...

How can I use multiple queryset in a ModelChoiceField?

I'm just beginning to learn Django and I would like to use different queryset in a ModelChoiceField. I have 3 models like that : class Politic(models.Model): name = models.CharField(max_length=100) class Economic(models.Model): name = models.CharField(max_length=100) class Category(models.Model): politic = models.ForeignKey(Politic,...

ModelForm save fails

Hi, I am trying to save a modelform that represents a bank account but I keep getting a ValueError even though the form appears to validate. The models I have to use are: class Person(models.Model): name = models.CharField() class Bank(models.Model): bsb = models.CharField() bank_name = models.CharField() def __uni...

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

Greetings, 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 sent correctly (i.e. that Primary key value doesn't exist yet). The problem is...

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 ManyToManyField to Factory. Now I want to construct a ModelForm for Bike but I want the...

Django's ModelForm unique_together validation

Hi, 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 = models.TextField(blank=True) date = models.DateTimeField(auto_now_add=True) class M...

Django model form using forms.ModelMultipleChoiceField

Hi - I have a ModelForm in my Django app that uses a forms.ModelMultipleChoiceField, which displays as a forms.CheckboxSelectMultiple widget on the form. This ModelForm is used to select/de-select values for a many-to-many relation. Here's the problem: when you uncheck all of the checkboxes and save the form, it doesn't save. If you un...

Dynamically update ModelForm’s Meta class model field

def SiteAdminForm(model_cls, *args, **kwargs): class MerchantAdminForm(forms.ModelForm): class Meta: exclude = ('external_links', 'published', 'logo','image_zip_file',) model = model_cls def __init__(self, *args, **kwargs): super(MerchantAdminForm, self).__init__(*args, **kwargs) ...

Django: How to Use upload_to=function with ModelForm

The goal is to dynamically update upload_to such that user uploaded files are stored in a directory location that depends on the user. There are several examples of this online, but none using ModelForm. See the code snippets for the two problems, one is that I am getting an empty string for the instance.user value, and when I try and ...

modelform "object not callable" error

Ok, so I'm fairly new to Django, but have been reading both the online django book and the djangoproject documentation, but I can't seem to figure this error out: I've got an 'Orders' model: class Orders(models.Model): client_id = models.ForeignKey(Client) order_date = models.DateField(auto_now_add = True) due_date = models...

After extending the User model in django, how do you create a ModelForm?

I extended the User model in django to include several other variables, such as location, and employer. Now I'm trying to create a form that has the following fields: First name (from User) Last name (from User) Location (from UserProfile, which extends User via a foreign key) Employer (also from UserProfile) I have created a modelfor...

Removing a fields from a dynamic ModelForm

In a ModelForm, i have to test user permissions to let them filling the right fields : It is defined like this: class TitleForm(ModelForm): def __init__(self, user, *args, **kwargs): super(TitleForm,self).__init__(*args, **kwargs) choices = ['','----------------'] # company if use...

django insert data from 2 tables using a single ModelForm

Hi there, I want to get data from 2 tables into a form using ModelForm, like this: fist model: class Cv(models.Model): created_by = models.ForeignKey(User, blank=True) first_name = models.CharField(('first name'), max_length=30, blank=True) last_name = models.CharField(('last name'), max_length=30, blank=True) url = mo...