Django维护个人信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/85224476

一 维护个人信息思维导图

二 个人信息数据模型

1 编辑个人信息的数据模型类型类

from django.db import models
from django.contrib.auth.models import User

# 所定义的模型类对应数据库中建立名为account_userProfile数据表
class UserProfile(models.Model):
    # 声明UserProfile类与User类之间的关系是“一对一”的
    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)

class UserInfo(models.Model):
    user = models.OneToOneField(User, unique=True)
    school = models.CharField(max_length=97, blank=True)
    company = models.CharField(max_length=97, blank=True)
    profession = models.CharField(max_length=27, blank=True)
    address = models.CharField(max_length=177, blank=True)
    # TextField对应HTML中的Textarea,blank为True表示可以为空
    aboutme = models.TextField(blank=True)  


    def __str__(self):
        return "user:{}".format(self.user.username)

2 生成数据库

(venv) E:\Django\mysite\mysite>python manage.py makemigrations
(venv) E:\Django\mysite\mysite>python manage.py migrate

3 查看生成的数据库

三 编辑表单类mysite/account/forms.py

from django import forms
# 引入Django默认的用户模型User类,在这个表的类中就应用User模型,不需要
# 再新建用户数据模型了
from django.contrib.auth.models import User
from .models import UserProfile,UserInfo

class UserInfoForm(forms.ModelForm):
    class Meta:
        model = UserInfo
        fields = ("school", "company", "profession", "address", "aboutme", "photo")

class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ("email",)

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/85224476