django-modelforms

Only validate certain fields if a BooleanField is set

Scenario: I'm building an order-form. Like every other order-form on the planet, it has separate invoicing shipping addresses. I've just added a "Use billing address" checkbox to let the user save time. The problem is, the shipping fields are still there. They will fail validation if the user don't enter any shipping address data (like ...

Django modelform: exclude fields by regex

I have a "Villa" Model with lots of descriptive TextFields. For each TextField, I have a copy which will be the Russian translation of the original field, which I'm naming by appending "_ru", for example "long_description" and "long_description_ru". I would like to exclude all the "_ru" fields from my ModelForm, which I thought I would b...

Django ModelForm Wizard - How to create the database object ?

Hello, I have a Model enought big to be cut in 3 Forms. I wanted to use FormWizzard to do that and I am wondering, how to save the information from the form to the database? Everything is from the same model. Do you have any idea of how to do that ? ...

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 display value of extra field in modelform

How do I set the value of a field (which is relevant to form, but not based directly on the same model) on a (model) form when it is displayed in the admin? Here is a simplified version of what I have (my actual app/model/etc is more complicated): A building has many rooms A room has many pieces of equipment Models: #spaces/model...

django modelform clean

I've found a few posts that are similar in nature to this but they haven't been 100% clear so here goes: In my views I have an add_album view that allows a user to upload an album. What I'd like to do is clean the form (AlbumForm) to check if this album is unique for an artist. My AlbumForm looks like this: class AlbumForm(ModelForm)...

I want to display name in front of field instead of....

..whatdoyoucallitanyway.. I have this model: class Kaart(models.Model): name = models.CharField(max_length=200, name="Kaardi peakiri", help_text="Sisesta kaardi pealkiri (maksimum tähemärkide arv on 38)", blank=False, null=False) url = models.CharField(max_length=200, blank=False, null=False, name="Asukoha URL", help_text="Täis...

Set attribute of a model to a certain value before saving.

I have this: class OrderForm(ModelForm): class Meta: model = Order exclude = ('number',) def __init__(self, *args, **kwargs): super(OrderForm, self).__init__(*args, **kwargs) if self.initial.get('account', None): self.fields['customer'].queryset = Customer.objects.filter(account=self....

Django form from related model

You have models: class Order(Model): date = DateField(editable = False, auto_now_add=True) status = CharField(max_length = 1, choices = STATUS, default = 'N') profile = ForeignKey(Profile, related_name = 'orders', blank = True, null = True) shipping = ForeignKey(Shipping, related_name = 'orders', blank = True, null = True) address ...

How do I dynamically exclude a non-model field from the admin?

I've added a custom field to a ModelForm for use in the admin: class MyModel(models.Model): name = models.CharField(max_length=64) ...etc... class MyModelAdminForm(forms.ModelForm): dynamicfield = forms.IntegerField() def __init__(self, *args, **kwargs): super(MyModelAdminForm, self).__init__(*args, **kwargs) ...

Validating data in Django ModelForm

And i have a simple modelform for Package from models import Package from django import forms class PackageForm(forms.ModelForm): class Meta: model= Package fields= ['name', 'version', 'url', 'description', 'arch', 'dependancies', 'conflicts', 'file'] How can i ask the modelform to check, within validation, if the file extens...

Django ModelForm for Many-to-Many fields

Consider the following models and form: class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: model = Topping When you view the Toppi...

Group form fields in django?

Hello, Is there a way in Django to group some fields from a ModelForm? For example, if there's a model with fields like: age, gender, dob, q1, q2, q3 and a form is created based in such Model, can I group the fields like: info_fields = (age, gender, dob) and response_fields = (q1, q2, q3). This would be helpful to display all fields in ...

Django ModelForm Template?

I want to learn how can I add to template to my ModelForm i'm newbie. Below you can see my models.py, url.py and views.py: My model.py looks like that: from django.db import models from django.forms import ModelForm from django.contrib.auth.models import User class Yazilar(models.Model): yazi = models.CharField(max_length=200)...

Django's forms.Form vs forms.ModelForm

Could anyone please explain to me similarities and differences of Django's forms.Form & forms.ModelForm? I've used Django for 4 days, so excuse me if I don't know basic things. Thanks in advance! ...

Django - Overriding the Model.create() method?

The Django docs only list examples for overriding save() and delete(). However, I'd like to define some extra processing for my models only when they are created. For anyone familiar with Rails, it would be the equivalent to creating a :before_create filter. Is this possible? ...

Extra parameter for Django models

Hello, With Django models, I want to achieve this: class Foo(models.Model): name = models.CharField(max_length=50) #wrapping the save function, including extra tasks def save(self, *args, **kwargs): super(Foo, self).save(*args, **kwargs) if extra_param: ...do task 1 else: ...

Django: Display Generic ModelForm or predefined form

I have 3 models with various fields in each. For 2 of the models, I'm fine with using a generic form (through Django's create_object) to request data. I wrote a function that accepts the model name and sends the user to the generic form url(r'^add_(?P<modelname>\w+)/$', generic_add), def generic_add(request, modelname): mdlnm_mod...

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

How to display a form filed with size and maxlength attributes in Django?

I have a model form that contains a DecimalField() with max_digits set to 5. How do I display this field this way: <input type"text" size="5" maxlength="5" /> ...