使用AbstractUser扩展默认的User表:

1、startapp创建account应用;

2、新增account的models:


from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser

class Users(AbstractUser):
    coin = models.IntegerField(default=0)

class Tasks(models.Model):
    title = models.CharField(max_length=100)

3、在settings.py中添加account到INSTALLED_APPS(这一点很重要,因为这个导致的错误浪费了大量时间),其它的官方文档中说的什么AUTH_USER_MODEL在这种应用场景下可以无视;

4、刷新数据库,python manage.py syncdb;

5、在其它应用的view中引用:


from account.models import *

try:
    user = Users.objects.get(username='user1')
except Users.DoesNotExist:
    user = None

if not user:
    user = Users.objects.create(username='user1', email='email1', password='password1', coin=100)
    user.save()

user.coin = 1000
user.save()

try:
    task = Tasks.objects.get(title='a task')
except Tasks.DoesNotExist:
    task = None

if not task:
    task = Tasks.objects.create(title='a task')
    task.save()

6、OK,下一步就是创建各种form各种view了。