convenience parent class for UserProfile model

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.db.models.base import ModelBase
from django.db import models

class _UserProfileModelBase(ModelBase):
    # _prepare is not part of the public API and may change
    def _prepare(cls):
        super(_UserProfileModelBase, cls)._prepare()

        def add_profile(sender, instance, created, **kwargs):
            if created:
                cls.objects.create(user=instance)

        # Automatically link profile when a new user is created
        post_save.connect(add_profile, sender=User, weak=False)

class UserProfileModel(models.Model):
    __metaclass__ = _UserProfileModelBase
    user = models.OneToOneField(User, primary_key=True, parent_link=True)

    class Meta:
        abstract = True

More like this

  1. Alternative to User.get_profile() by jpwatts 5 years, 2 months ago
  2. Improved User Admin by gregb 3 years ago
  3. User post_save signal to auto create 'admin' profile by marinho 5 years, 5 months ago
  4. Active User Sorted ModelAdmin by daemondazz 3 years, 10 months ago
  5. Multiple User subclasses custom Auth backend by ungenio41 1 year, 8 months ago

Comments

(Forgotten your password?)