Project - Create Application | Create a table

User table and user application module

Create a user application module

Premise: In a virtual environment in luffy

1 . Project from the terminal into the root directory apps
 >: CD & luffyapi CD apps

2 Create App
 >: Python ../../manage.py startapp the User 
# to create applications based on manage.py command execution, so to find manage.py

Create a User table corresponding model: user / models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    mobile = models.CharField(max_length=11, unique=True)
    icon = models.ImageField(upload_to='icon', default='icon/default.png')

    class Meta:
        db_table = 'luffy_user'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username

Registered user module configured to the User table: dev.py

INSTALLED_APPS = [
    # ...
    'user',
]

# Custom User table User table because the table is based on auth_user create 
AUTH_USER_MODEL = ' user.user '

Configuring media

media configuration: dev.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
media directory configuration
"""
├── luffyapi
    └── luffyapi /
        └── media/      
            └── icon 
                └── default.png
"""

Database Migration

# Data migration is based on manage.py command execution, so to find manage.py 
migrate command:
python manage.py makemigrations 
python manage.py migrate

 

 Note: Pillow module will report an error when migrating, because the user table

Pillow module mounting
 >: pip install Pillow

 

Guess you like

Origin www.cnblogs.com/waller/p/11740976.html