zmm064
4/14/2018 - 8:15 AM

扩展已有项目的User model

扩展已有项目的User model

from django.conf import settings
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.


class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    city = models.CharField(max_length=120, null=True, blank=True)

    def __str__(self):
        return str(self.user.username)

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


# def post_save_user_model_receiver(sender, instance, created, *args, **kwargs):
#     if created:
#         try:
#             Profile.objects.create(user=instance)
#         except:
#             pass

# post_save.connect(post_save_user_model_receiver, sender=settings.AUTH_USER_MODEL)

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def post_save_user_model_receiver(sender, instance, **kwargs):
    try:
        Profile.objects.create(user=instance)
    except:
        pass