how to create a custom user model in django code example
Example 1: django create user
from django.contrib.auth.models import User
user = User.objects.create_user(value['USERNAME'], value['EMAIL'], value['PASSWORD'])
user.first_name = value['NAME']
user.last_name = value['SURNAME']
user.save()
Example 2: Django Custom user model
...
class User(AbstractUser):
def __unicode__(self):
return self.username
balance = models.IntegerField(default=0)
total_pledged = models.IntegerField(default=0)
last_pledged = models.ForeignKey('Transaction', related_name='pledger', blank=True, null=True)
extension_key = models.CharField(max_length=100, null=True, blank=True)
plugin_key = models.CharField(max_length=100, null=True, blank=True)
ghosted = models.BooleanField(default=False)
def save(self, *args, **kwargs):
print('saving')
try:
self.company.save()
except:
print('no company')
super(User, self).save(*args, **kwargs)
...
Example 3: Django Custom User Model
ChangeSwappableModel(
setting="AUTH_USER_MODEL",
old="auth.User",
new="my_auth.User"
)
Example 4: Django Custom User Model
./manage.py shell
>>>
>>> from django.contrib.admin.models import LogEntry
>>> from django.contrib.contenttypes.models import ContentType
>>>
>>> auth_user = ContentType.objects.get(app_label='auth', model='user')
>>> accouts_user = ContentType.objects.get(app_label='Accounts', model='user')
>>>
>>> for le in LogEntry.objects.filter(content_type=auth_user):
... le.content_type = accouts_user
... le.save()
...
Example 5: django customize the user model
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=100)