VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • python3 之 天天生鲜 项目(初学者)2

创建完第一个 users 程序后  在创建第二个程序 发现 报错了  “没有users模块”

复制代码
(django_py3) czpubuntu@czpubuntu:~/PycharmProjects/Fresh_every_day/apps$ python ../manage.py startapp goods
Traceback (most recent call last):
  File "../manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/czpubuntu/.virtualenvs/django_py3/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/czpubuntu/.virtualenvs/django_py3/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
    django.setup()
  File "/home/czpubuntu/.virtualenvs/django_py3/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/czpubuntu/.virtualenvs/django_py3/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/czpubuntu/.virtualenvs/django_py3/lib/python3.6/site-packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'users'
复制代码

在settings.py中添加   apps 路径   解决啦

import sys

sys.path.insert(1,os.path.join(BASE_DIR,'apps'))

 

 

 

 创建了4大模块--------------------------用户模块、商品模块、购物车模块、订单模块 

 完成OK-------------------------

 

models.py          模型的创建

创建一个utils(工具包)   有什么用?后面的模型都会继承于它,经常被使用所以简化一个内容    不要也无所谓    复制粘贴也很方便

复制代码
from django.db import models

class BaseModel(models.Model):
    '''模型抽象基类'''
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    class Meta:
        # 说明是一个抽象模型类
        abstract = True
复制代码

 

 

 users -- models.py

复制代码
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings
from utils.base_model import BaseModel


# Create your models here.

class User(AbstractUser, BaseModel):
    '''用户模型类'''
   #create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
   #update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    #is_delete = models.BooleanField(default=False, verbose_name='删除标记')

	
    class Meta:
        db_table = 'df_user'  # 数据库中的名字
        verbose_name = '用户'  # admin页面上的名字 
        verbose_name_plural = verbose_name


class Address(BaseModel):
    '''地址模型类'''
    user = models.ForeignKey('User', verbose_name='所属账户')
    receiver = models.CharField(max_length=20, verbose_name='收件人')
    addr = models.CharField(max_length=256, verbose_name='收件地址')
    zip_code = models.CharField(max_length=6, null=True, verbose_name='邮政编码')
    phone = models.CharField(max_length=11, verbose_name='联系电话')
    is_default = models.BooleanField(default=False, verbose_name='是否默认')

    class Meta:
        db_table = 'df_address'
        verbose_name = '地址'
        verbose_name_plural = verbose_name  # 不添加  admin显示地址后会多个s

    def __str__(self):
        return self.receiver
复制代码

goods --- models.py

复制代码
from django.db import models
from utils.base_model import BaseModel
from tinymce.models import HTMLField

# Create your models here.

class GoodsCategory(BaseModel):
    """商品类别表"""
    name = models.CharField(max_length=20, verbose_name="名称")
    logo = models.CharField(max_length=100, verbose_name="标识")
    image = models.ImageField(upload_to="category", verbose_name="图片")

    class Meta:
        db_table = "df_goods_category"
        verbose_name = "商品类别"  # admin站点使用
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Goods(BaseModel):
    """商品SPU表"""
    name = models.CharField(max_length=100, verbose_name="名称")
    desc = HTMLField(verbose_name="详细介绍", default="", blank=True)

    class Meta:
        db_table = "df_goods"
        verbose_name = "商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class GoodsSKU(BaseModel):
    """商品SKU表"""
    category = models.ForeignKey(GoodsCategory, verbose_name="类别")
    goods = models.ForeignKey(Goods, verbose_name="商品")
    name = models.CharField(max_length=100, verbose_name="名称")
    title = models.CharField(max_length=200, verbose_name="简介")
    unit = models.CharField(max_length=10, verbose_name="销售单位")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="价格")
    stock = models.IntegerField(default=0, verbose_name="库存")
    sales = models.IntegerField(default=0, verbose_name="销量")
    default_image = models.ImageField(upload_to="goods", verbose_name="图片")
    status = models.BooleanField(default=True, verbose_name="是否上线")

    class Meta:
        db_table = "df_goods_sku"
        verbose_name = "商品SKU"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class GoodsImage(BaseModel):
    """商品图片"""
    sku = models.ForeignKey(GoodsSKU, verbose_name="商品SKU")
    image = models.ImageField(upload_to="goods", verbose_name="图片")

    class Meta:
        db_table = "df_goods_image"
        verbose_name = "商品图片"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.sku)

class IndexGoodsBanner(BaseModel):
    """主页轮播商品展示"""
    sku = models.ForeignKey(GoodsSKU, verbose_name="商品SKU")
    image = models.ImageField(upload_to="banner", verbose_name="图片")
    index = models.SmallIntegerField(default=0, verbose_name="顺序")

    class Meta:
        db_table = "df_index_goods"
        verbose_name = "主页轮播商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.sku)

class IndexCategoryGoodsBanner(BaseModel):
    """主页分类商品展示"""
    DISPLAY_TYPE_CHOICES = (
        (0, "标题"),
        (1, "图片")
    )
    category = models.ForeignKey(GoodsCategory, verbose_name="商品类别")
    sku = models.ForeignKey(GoodsSKU, verbose_name="商品SKU")
    display_type = models.SmallIntegerField(choices=DISPLAY_TYPE_CHOICES, verbose_name="展示类型")
    index = models.SmallIntegerField(default=0, verbose_name="顺序")

    class Meta:
        db_table = "df_index_category_goods"
        verbose_name = "主页分类展示商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.sku)

class IndexPromotionBanner(BaseModel):
    """主页促销活动展示"""
    name = models.CharField(max_length=50, verbose_name="活动名称")
    url = models.URLField(verbose_name="活动连接")
    image = models.ImageField(upload_to="banner", verbose_name="图片")
    index = models.SmallIntegerField(default=0, verbose_name="顺序")

    class Meta:
        db_table = "df_index_promotion"
        verbose_name = "主页促销活动"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
复制代码

orders -------models.py

复制代码
from django.db import models

from utils.base_model import BaseModel
from users.models import User, Address
from goods.models import GoodsSKU

# Create your models here.


class OrderInfo(BaseModel):
    """订单信息"""
    PAY_METHODS = {
        1: "货到付款",
        2: "支付宝",
    }

    PAY_METHODS_ENUM = {
        "CASH": 1,
        "ALIPAY": 2
    }

    PAY_METHOD_CHOICES = (
        (1, "货到付款"),
        (2, "支付宝"),
    )

    ORDER_STATUS = {
        1: "待支付",
        2: "待发货",
        3: "待收货",
        4: "待评价",
        5: "已完成",
    }

    ORDER_STATUS_ENUM = {
        "UNPAID": 1,
        "UNSEND": 2,
        "UNRECEIVED": 3,
        "UNCOMMENT": 4,
        "FINISHED": 5
    }

    ORDER_STATUS_CHOICES = (
        (1, "待支付"),
        (2, "待发货"),
        (3, "待收货"),
        (4, "待评价"),
        (5, "已完成"),
    )

    order_id = models.CharField(max_length=64, primary_key=True, verbose_name="订单号")
    user = models.ForeignKey(User, verbose_name="下单用户")
    address = models.ForeignKey(Address, verbose_name="收获地址")
    total_count = models.IntegerField(default=1, verbose_name="商品总数")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品总金额")
    trans_cost = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="运费")
    pay_method = models.SmallIntegerField(choices=PAY_METHOD_CHOICES, default=1, verbose_name="支付方式")
    status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name="订单状态")
    trade_id = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="支付编号")

    class Meta:
        db_table = "df_order_info"
        verbose_name = "订单信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.address


class OrderGoods(BaseModel):
    """订单商品"""
    order = models.ForeignKey(OrderInfo, verbose_name="订单")
    sku = models.ForeignKey(GoodsSKU, verbose_name="订单商品")
    count = models.IntegerField(default=1, verbose_name="数量")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="单价")
    comment = models.TextField(default="", verbose_name="评价信息")

    class Meta:
        db_table = "df_order_goods"
        verbose_name = "订单商品"
        verbose_name_plural = verbose_name
复制代码

 

模型创建完成  ------ 生成迁移文件 --------- 迁移到数据库

 生成迁移文件发现抱错了

复制代码
(django_py3) czpubuntu@czpubuntu:~/PycharmProjects/Fresh_every_day/apps$ python ../manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
users.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
users.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
复制代码

在settings.py 中添加 

#引导
AUTH_USER_MODEL = 'users.User'

 

 解决啦

(django_py3) czpubuntu@czpubuntu:~/PycharmProjects/Fresh_every_day/apps$ python ../manage.py migrate                #  完成数据迁移

 

 OK ------------------------------------------------
 

出处:https://www.cnblogs.com/Py-beginner/p/13816066.html


 


相关教程