Logo F2FInterview

Django Interview Questions

Q   |   QA

Django User model has its own limitations. We cann't extend fields directly in User Model without change default architecture. But we can map another model to Django User model.

The steps as follows:

Step1: Settings.py

AUTH_PROFILE_MODULE = 'appName.UserProfile'

Step2: Models.py

class UserProfile(models.Model):
     user = models.ForeignKey(User,unique=True)
     mobile_number = models.CharField(max_length = 12, verbose_name=_("Mobile Phone"),help_text=_("Enter a valid mobile number with country code (Example: +919884620444)"))

     #additional info
     photo = models.ImageField(upload_to='img-data/userphoto',default='img-data/userphoto/no_profilephoto.png',blank=True)
     dob = models.DateField(auto_now=True)
     contact_address_line1 = models.CharField(max_length=140,blank=True)
     contact_address_line2 = models.CharField(max_length=140,blank=True)
     contact_address_line3 = models.CharField(max_length=140,blank=True)
     city = models.CharField(max_length = 140,blank=True)
     country = models.ForeignKey(Country)
     state = models.CharField(max_length = 140,blank=True)
     zipcode = models.IntegerField(default = 0)
     latitude = models.FloatField(default = 0.0)
     longitude = models.FloatField(default = 0.0)

     def __unicode__(self):
          return self.user.username

     class Meta:
          db_table = 'userprofile'
          verbose_name = _('userprofile')
          verbose_name_plural = _('userprofiles')
          ordering = ('user',) 

Step3: For get User Profile Info

userprofile = user.get_profile()       

Set fieldsets to control the layout of admin "add" and "change" pages.

fieldsets is a list of two-tuples, in which each two-tuple represents a <fieldset> on the admin form page. (A <fieldset> is a "section" of the form.)

The two-tuples are in the format (name, field_options), where name is a string representing the title of the fieldset and field_options is a dictionary of information about the fieldset, including a list of fields to be displayed in it.

For example:

Models.py

class UserProfileInfo(models.Model):
    name = models.CharField(max_length=50)
    age = models.IntegerField(max_length=3)
    phone = models.CharField(max_length=13)
    marital_status = models.CharField(max_length=140,default='',choices=(
                                      ('Single','Single'),
                                      ('Married with children','Married with children'),
                                      ('Divorced','Divorced'),
                                      ('Separated','Separated'),
                                      ('Widowed','Widowed')),blank=True)
    contact_address_line1 = models.CharField(max_length=140,blank=True)
    contact_address_line2 = models.CharField(max_length=140,blank=True)
    contact_address_line3 = models.CharField(max_length=140,blank=True)
    city = models.CharField(max_length = 140,blank=True)
    country = models.CharField(max_length = 140,blank=True)
    state = models.CharField(max_length = 140,blank=True)
    zipcode = models.IntegerField(default = 0)

    def __unicode__(self):
        return "%s" % (self.name)

    class Meta:
        db_table = 'UserProfileInfo'

Admin.py

from django.contrib import admin
from models import UserProfileInfo
class UserProfileInfoAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('name', 'age', 'marital_status')
        }),
        ('Contact Info', {
            'fields': ('contact_address_line1', 'contact_address_line2', 'contact_address_line3','city','country','state','zipcode')
        }),
    )
admin.site.register(UserProfileInfo,UserProfileInfoAdmin)

output:

Admin Model Fieldsets

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: