django-forms

How to markup form fields with <div class='field_type'> in Django

I wasn't able to find a way to identify the type of a field in a django template. My solution was to create a simple filter to access the field and widget class names. I've included the code below in case it's helpful for someone else. Is there a better approach? ## agency/tagutils/templatetags/fieldtags.py ##########################...

Unique BooleanField value in Django?

Suppose my models.py is like so: class Character(models.Model): name = models.CharField(max_length=255) is_the_chosen_one = models.BooleanField() I want only one of my Character instances to have is_the_chosen_one == True and all others to have is_the_chosen_one == False . How can I best ensure this uniqueness constraint is re...

ModelForm to OneToMany relationship

I need create the following relationship: One "Rule" can have many users, but one user can be only one rule. Using ForeignKey and a ModelForm, I get a select box to select just ONE user, but I want to select many users. It's not a ManyToMany relationship because one user never can be more than one rule. Here are my model definitions:...

Django Comments: Want to remove user URL, not expand the model. How to?

I'm totally understanding the documentation on expanding the Comments app in Django, and really would like to stick with the automatic functionality but... In the current app, I have absolutely no use for an "URL" to be submitted along with a comment. Being minimally invasive of the default setup, how can I prevent this field from sho...

Django forms: how to display media (javascript) for a DateTimeInput widget ?

Hello (please excuse me for my bad english ;) ), Imagine the classes bellow: models.py from django import models class MyModel(models.Model): content_type = models.ForeignKey(ContentType, verbose_name=_('content type')) object_id = models.PositiveIntegerField(_('object id')) content_object = generic.GenericForeignKey('con...

OneToMany in Django-Forms

Hi there, Django has the StackedInline-Feature for the Admin-Backend - is there any equivalent for django.forms? When i define a ForeignField representing a ManyToOne-Relation, it does not show up in generated forms. ...

django model Form. Include fields from related models

Hi. I have a model, called Student, which has some fields, and a OneToOne relationship with user (django.contrib.auth.User). class Student(models.Model): phone = models.CharField(max_length = 25 ) birthdate = models.DateField(null=True) gender = models.CharField(max_length=1,choices = GENDER_CHOICES) city = models.C...

save method in a view

I have a very simple model: class Artist(models.Model): name = models.CharField(max_length=64, unique=False) band = models.CharField(max_length=64, unique=False) instrument = models.CharField(max_length=64, unique=False) def __unicode__ (self): return self.name that I'm using as a model form: from django.forms import ModelForm...

How to approach creating Related Links generic (like Comments/Tags) in Django

Since I have not found a Related Links app that works with Django 1.0/trunk, I was looking to create my own. I would like to attach "Related Links" to models in the same generic way that Comments framework or Tags work. I've looked over the Content Types documentation but can't wrap my head around (nor find much documentation for) ho...

How to build flexible inline formsets?

I have a complex form containing an inline formset, which basically has got some text fields and a file upload field. Now, I want to enable the user to create a new record, and within the same step attach several files. I think there are different options to achieve this, maybe I could write a jQuery-Plugin that clones the formset, do a...

Django: form values not updating when model updates

I am creating a form that uses MultipleChoiceField. The values for this field are derived from another model. This method works fine, however, I am noticing (on the production server) that when I add a new item to the model in question (NoticeType), the form does not dynamically update. I have to restart the server for the new item to...

django forms wizard and recaptcha

I did a recaptcha integration using the following django snippet settings.py RECAPTCHA_PUBLIC_KEY = '<your public key>' RECAPTCHA_PRIVATE_KEY = '<your private key>' #widgets.py from django import forms from django.utils.safestring import mark_safe from django.conf import settings from recaptcha import captcha...

Tutorial about how to write custom form fields in django?

Is there any good articles that explain custom form fields in django, not custom model fields? I couldn't find any through google. ...

Django: ajax response for valid/available username/email during registration

Hello - I am using jQuery to do some inline form validation during user registration to prevent form errors after posting by checking to see if: username is available email has not already been registered The idea is to give the user feedback before the form is submitted to prevent frustration. The code is at the bottom. Question...

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

Django form with just a BooleanField

I'm rather new to Django and I'm using Django 1.0. I have this: forms.py: class MyForm(forms.Form): extra_cheeze = forms.BooleanField(required=False, initial=False, label='Extra cheeze') views.py: def order_something(request): form = MyForm(request.PO...

Date format for django's SelectDateWidget

SelectDateWidget is very convenient but it normally seems to return dates in the format "%Y-%m-%d". It doesn't take a format parameter and doesn't have much documentation. Anyone run into this or have an idea how to work around it to get the output of the date into another format? There is this ticket, #6231 saying that there's a fix to ...

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

Custom form in inline form

I have a custom form to display goals. Goals are edited inline in a Game. class GoalForm(forms.ModelForm): class Meta: model = Goal def __init__(self, *args, **kwargs): super(GoalForm, self).__init__(*args, **kwargs) self.fields['goal_scorer'].queryset = Player.objects.filter(gameroster__game=self.instance.g...

what is the easiest way to set a default choice for a django select widget?

I have a model that has a foreign key relation to another. I'd like to have the dropdown that is produced by widget=forms.Select have a default value ...