Search code examples
pythondjangodjango-registration

Django: Add another field to django-registration


I am pretty new to django and I am very lost with django-registration. At the moment I have the django-registration set up an going but I need to add another field to it for a phone number. I need the phone number field in the registration field instead so I can use the api from twilio to send a verification link through text instead of email. How would I go about adding this one field to django-registration?


Solution

  • I work with django at work and for that kind of issue we used to attach a model to the user, example:

    • You create a new model, for example profile with a OneToOneField to user
    • Add your desired fields to that profile model, such as (tlf, country, language,log...)
    • Create admin.py to manage this model (profile) at the same time you manage users in django admin

    Profile Model Example

    class Profile(models.Model):
        user = models.OneToOneField(User)
        phone = models.CharField(max_length=255, blank=True, null=True, verbose_name='phone')
        description = models.TextField(blank=True, verbose_name='descripction')
        ...
        ...
        class Meta:
            ordering = ['user']
            verbose_name = 'user'
            verbose_name_plural = 'users'
    

    admin.py example

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals    
    from django.contrib import admin
    from django.contrib.auth.admin import UserAdmin
    from django.contrib.auth.models import User
    
    
    class ProfileInline(admin.StackedInline):
        model = Profile
        can_delete = False
        filter_horizontal = ['filter fields']  # example: ['tlf', 'country',...]
        verbose_name_plural = 'profiles'
        fk_name = 'user'
    
    class UserAdmin(UserAdmin):
        inlines = (ProfileInline, )
        list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
        list_filter = ('is_staff', 'is_superuser', 'is_active')
    
    admin.site.unregister(User)  # Unregister user to add new inline ProfileInline
    admin.site.register(User, UserAdmin)  # Register User with this inline profile
    

    Create an user and attach a profile to him

    # Create user
    username = 'TestUser'
    email = 'test@example.com'
    passw = '1234'  
    new_user = User.objects.create_user(username, email, passw)
    
    # Create profile
    phone = '654654654'
    desc = 'Test user profile'
    new_profile = Profile(user=new_user, phone = phone, description=desc)
    new_profile.profile_role = new_u_prole
    new_profile.user = new_user
    
    # Save profile and user
    new_profile.save()
    new_user.save()
    

    Now you'll have this Profile model attached to each user, and you could add the fields you wish to Profile Model, and for example if you make:

    user = User.objects.get(id=1)

    you can access to his profile doing:

    user.profile

    and to access phone

    user.profile.phone