Django增加注册内容
2018年2月7日编辑/account/models.py
1 2 3 4 5 6 7 8 9 10 11 |
from django.db import models from django.contrib.auth.models import User # Create your models here. class UserProfile(models.Model): #声明数据库的名字 user = models.OneToOneField(User, unique=True) #定义一对一 birth = models.DateField(blank=True, null=True) phone = models.CharField(max_length=20, null=True) def __str__(self): return 'user {}'.format(self.user.username) |
执行命令会生成account/migrations/0001_initial.py这个文件
1 |
$ python manage.py makemigrations account |
执行命令创建数据库了
1 |
$ python manage.py migrate account |
接下来查看我们创建的数据库
好了接下来我们编写表单类account/forms.py
1 2 3 4 5 6 7 8 9 |
from django import forms from django.contrib.auth.models import User #引入默认用户模型user类 from .models import UserProfile #引入UserProfile模型 class UserProfileForm(forms.ModelForm): class Meat: model = UserProfile fields = ("phone","birth") ... |