Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 1.96 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Django - Create user profile on user creation
  2. # -*- coding: utf-8 -*-
  3. from django.db import models
  4. from django.db.models.signals import post_save
  5. from django.contrib.auth.models import User
  6. from main.models import Store
  7.  
  8. class UserProfile(models.Model):
  9.  
  10.     GENRE_CHOICES = (
  11.         ('m', 'Masculino'),
  12.         ('f', 'Feminino'),
  13.     )
  14.     MARITAL_STATUS_CHOICES = (
  15.         ('s', 'Solteiro'),
  16.         ('c', 'Casado'),
  17.         ('d', 'Divorciado'),
  18.         ('v', 'Viúvo'),
  19.     )
  20.  
  21.     user = models.ForeignKey(User, unique=True)
  22.     birth_date = models.DateField()
  23.     genre = models.CharField(max_length=1, choices=GENRE_CHOICES)
  24.     address = models.CharField(max_length=150)
  25.     postal_code_4 = models.PositiveIntegerField()
  26.     postal_code_3 = models.PositiveIntegerField()
  27.     locatity = models.CharField(max_length=30)
  28.     marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES)
  29.     child_amount = models.PositiveSmallIntegerField()
  30.     is_merchant = models.BooleanField(default=False)
  31.     store = models.ForeignKey(Store, null=True)
  32.  
  33. def create_user_profile(sender, instance, created, **kwargs):
  34.     if created:
  35.         UserProfile.objects.create(user=instance)
  36.  
  37. post_save.connect(create_user_profile, sender=User)
  38.        
  39. /djangoProjects/lwboanova/lwboanova/apps/accounts/models.py in create_user_profile
  40.  
  41. 34: UserProfile.objects.create(user=instance)
  42.        
  43. user = models.ForeignKey(User, unique=True)
  44. birth_date = models.DateField(null=True)
  45. genre = models.CharField(max_length=1, choices=GENRE_CHOICES, null=True)
  46. address = models.CharField(max_length=150, null=True)
  47. postal_code_4 = models.PositiveIntegerField(null=True)
  48. postal_code_3 = models.PositiveIntegerField(null=True)
  49. locatity = models.CharField(max_length=30, null=True)
  50. marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES, null=True)
  51. child_amount = models.PositiveSmallIntegerField(null=True)
  52. is_merchant = models.BooleanField(default=False)
  53. store = models.ForeignKey(Store, null=True)