Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding extra fields to django-registration form

I have a model called "Organization" that I've setup as a User profile and I would like to have the fields from the "Organization" model show up on the registration page. How do I go about doing this with django-registration.

# models.py
class Organization(models.Model):
    user = models.ForeignKey(User, unique=True)
    logo = models.ImageField(upload_to='organizations')
    name = models.CharField(max_length=100, null=True, unique=True)

    # more fields below etc.

# settings.py
AUTH_PROFILE_MODULE = 'volunteering.organization'
like image 675
Garth Humphreys Avatar asked Feb 25 '11 20:02

Garth Humphreys


People also ask

How do I add extra fields to a registration form in WordPress?

So to begin, you'll need to add custom profile fields to your WordPress website. To do that, go to Custom Fields » Add New. Then give your field group a name like “User Profile.” After that, click Add New to add a field to that group and enter the name and label details.


1 Answers

The easiest way to do this would be [tested on django-registration 0.8]:

Somewhere in your project, say forms.py in your organization app

from registration.forms import RegistrationForm
from django.forms import ModelForm
from models import Organization

class OrganizationForm(forms.ModelForm):
    class Meta:
        model = Organization

RegistrationForm.base_fields.update(OrganizationForm.base_fields)

class CustomRegistrationForm(RegistrationForm):
    def save(self, profile_callback=None):
        user = super(CustomRegistrationForm, self).save(profile_callback=None)
        org, c = Organization.objects.get_or_create(user=user, \
            logo=self.cleaned_data['logo'], \
            name=self.cleaned_data['name'])

Then in your root urlconf [but above the regex pattern that includes registration.urls and assuming that regex is r'^accounts/'] add:

from organization.forms import CustomRegistrationForm

urlpatterns += patterns('',
    (r'^accounts/register/$', 'registration.views.register',    {'form_class':CustomRegistrationForm}),
)

Obviously, you can also create a custom backend, but IMHO this is way easier.

like image 112
Simon Kagwi Avatar answered Oct 26 '22 18:10

Simon Kagwi