- Django - Create user profile on user creation
- # -*- coding: utf-8 -*-
- from django.db import models
- from django.db.models.signals import post_save
- from django.contrib.auth.models import User
- from main.models import Store
- class UserProfile(models.Model):
- GENRE_CHOICES = (
- ('m', 'Masculino'),
- ('f', 'Feminino'),
- )
- MARITAL_STATUS_CHOICES = (
- ('s', 'Solteiro'),
- ('c', 'Casado'),
- ('d', 'Divorciado'),
- ('v', 'Viúvo'),
- )
- user = models.ForeignKey(User, unique=True)
- birth_date = models.DateField()
- genre = models.CharField(max_length=1, choices=GENRE_CHOICES)
- address = models.CharField(max_length=150)
- postal_code_4 = models.PositiveIntegerField()
- postal_code_3 = models.PositiveIntegerField()
- locatity = models.CharField(max_length=30)
- marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES)
- child_amount = models.PositiveSmallIntegerField()
- is_merchant = models.BooleanField(default=False)
- store = models.ForeignKey(Store, null=True)
- def create_user_profile(sender, instance, created, **kwargs):
- if created:
- UserProfile.objects.create(user=instance)
- post_save.connect(create_user_profile, sender=User)
- /djangoProjects/lwboanova/lwboanova/apps/accounts/models.py in create_user_profile
- 34: UserProfile.objects.create(user=instance)
- user = models.ForeignKey(User, unique=True)
- birth_date = models.DateField(null=True)
- genre = models.CharField(max_length=1, choices=GENRE_CHOICES, null=True)
- address = models.CharField(max_length=150, null=True)
- postal_code_4 = models.PositiveIntegerField(null=True)
- postal_code_3 = models.PositiveIntegerField(null=True)
- locatity = models.CharField(max_length=30, null=True)
- marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES, null=True)
- child_amount = models.PositiveSmallIntegerField(null=True)
- is_merchant = models.BooleanField(default=False)
- store = models.ForeignKey(Store, null=True)