4-5 自定义userprofile

新建app


命令:startapp users



第一步:

# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals

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

# Create your models here.


class UserProfile(AbstractUser):
    nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
    birday = models.DateField(verbose_name=u"生日", null=True, blank=True)
    gender = models.CharField(choices=(("male",u"男"),("female","女")), default="female")
    address = models.CharField(max_length=100, default=u"")
    mobile = models.CharField(max_length=11, null=True, blank=True)
    image = models.ImageField(upload_to="image/%Y/%m",default=u"image/default.jpg")

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.username








第二步:

在setting注册新建的app


报错:

ERRORS:
users.UserProfile.gender: (fields.E120) CharFields must define a 'max_length' attribute.
users.UserProfile.image: (fields.E210) Cannot use ImageField because Pillow is not installed.
	HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install Pillow".

System check identified 2 issues (0 silenced).
ERRORS:
users.UserProfile.image: (fields.E210) Cannot use ImageField because Pillow is not installed.
	HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install Pillow".

安装:pip install Pillow



重新运行项目

pydev debugger: process 7728 is connecting

Performing system checks...

System check identified no issues (0 silenced).
Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace instance at 0x029A8EE0>
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 589, in __call__
    return self.original_func(*self.args, **self.kwargs)
  File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check_migrations()
  File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\core\management\commands\runserver.py", line 163, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\db\migrations\executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
    self.build_graph()
  File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\db\migrations\loader.py", line 299, in build_graph
    parent = self.check_key(parent, key[0])
  File "C:\Users\hlg\Envs\mxonline\lib\site-packages\django\db\migrations\loader.py", line 160, in check_key
    raise ValueError("Dependency on app with no migrations: %s" % key[0])
ValueError: Dependency on app with no migrations: users

解决办法:


输入命令:


生成数据表:

migrate users


输入:yes




































































猜你喜欢

转载自blog.csdn.net/huanglianggu/article/details/80454327
4-5