超越村后端开发(2:新建models.py+xadmin的引入)

1.新建Model

1.users数据

1.在apps/users/models.py中:

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

# Create your models here.

class UserProfile(AbstractUser):
    """
    管理员
    """
    name=models.CharField(max_length=30,null=True,blank=True,verbose_name=‘姓名‘)
    mobile=models.CharField(max_length=11,null=True,blank=True,verbose_name=‘电话‘)

    class Meta:
        verbose_name=‘管理员‘
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username

class WxUsers(models.Model):
    """
    微信用户
    """
    open_id=models.CharField(max_length=64,null=True,blank=True,verbose_name=‘OpenId‘)
    token=models.CharField(max_length=64,null=True,blank=True,verbose_name=‘token‘)
    nickName=models.CharField(max_length=30,null=True,blank=True,verbose_name=‘微信名‘)
    avatarUrl=models.CharField(max_length=200,null=True,blank=True,verbose_name=‘用户头像URL‘)
    gender = models.CharField(max_length=4, choices=((‘0‘, ‘未知‘),(‘1‘, ‘男‘), (‘2‘, ‘女‘)), default=‘0‘,
                              verbose_name=‘性别‘)
    province=models.CharField(max_length=20,null=True,blank=True,verbose_name=‘用户所在省‘)
    city=models.CharField(max_length=20,null=True,blank=True,verbose_name=‘用户所在市‘)
    country=models.CharField(max_length=20,null=True,blank=True,verbose_name=‘用户所在旗县区‘)
    longitude=models.CharField(max_length=10,null=True,blank=True,verbose_name=‘经度‘)
    latitude=models.CharField(max_length=10,null=True,blank=True,verbose_name=‘纬度‘)
    last_login_time = models.DateTimeField(default=datetime.now, verbose_name="最后登录时间")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name=‘微信用户‘
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.nickName

2.在settings.py中增加配置项:

AUTH_USER_MODEL=‘users.UserProfile‘

3.执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

2.wish数据

1.apps/wish/models.py中:

from django.db import models
from users.models import WxUsers,UserProfile
from datetime import datetime
from django.utils.safestring import mark_safe
# Create your models here.

class Wish(models.Model):
    """
    愿望表
    """
    wxuser=models.ForeignKey(WxUsers,on_delete=models.CASCADE)
    content=models.CharField(max_length=200,default=‘‘,verbose_name=‘愿望内容‘,help_text=‘最多200字‘)
    status=models.CharField(max_length=8, choices=((‘0‘, ‘进行中‘),(‘1‘, ‘放弃了‘), (‘2‘, ‘超时了‘)), default=‘0‘,
                              verbose_name=‘阶段‘)
    pv_num=models.IntegerField(default=0,verbose_name=‘浏览数‘)
    up_num=models.IntegerField(default=0,verbose_name=‘点赞数‘)
    share_num=models.IntegerField(default=0,verbose_name=‘转发数‘)
    bb_num=models.IntegerField(default=0,verbose_name=‘评论数‘)
    end_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name=‘愿望表‘
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.content

class News(models.Model):
    """
    新闻表
    """
    user=models.ForeignKey(UserProfile,on_delete=models.CASCADE)
    banner=models.ImageField(max_length=200, upload_to=‘banner/‘,verbose_name=‘轮播图‘)
    title=models.CharField(max_length=15,default=‘‘,verbose_name=‘新闻标题‘,help_text=‘最多15字‘)
    content=models.CharField(max_length=800,default=‘‘,verbose_name=‘新闻正文‘,help_text=‘最多800字‘)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name=‘新闻表‘
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.title

    def image_url(self):
        return mark_safe(‘<img src="/media/{0}" class="field_img">‘.format(self.banner))

    image_url.short_description = ‘轮播图‘

class Images(models.Model):
    """
    新闻图片
    """
    image = models.ImageField(max_length=200, upload_to=‘images/‘)
    news=models.ForeignKey(News, verbose_name="新闻", blank=True,null=True,on_delete=models.CASCADE)
    add_time = models.DateTimeField(default=datetime.now, verbose_name=‘添加时间‘)

    class Meta:
        verbose_name = "新闻图片"
        verbose_name_plural = verbose_name

    def image_url(self):
        return mark_safe(‘<img src="/media/{0}" class="field_img">‘.format(self.image))

    image_url.short_description = ‘图片‘

2.安装 pillow包(做图片处理的)

pip install pillow

3.执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

3.user_operation数据

1.apps/user_operation/models.py中:

from django.db import models
from datetime import datetime
from users.models import WxUsers,UserProfile
from wish.models import Wish
# Create your models here.

class Message(models.Model):
    """
    评论表,消息表
    """
    user_send=models.ForeignKey(WxUsers,on_delete=models.CASCADE,verbose_name=‘发出方‘,related_name=‘send‘)
    user_receive = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name=‘接收方‘, related_name=‘receive‘)
    wish=models.ForeignKey(Wish,on_delete=models.CASCADE,verbose_name=‘被评论的愿望‘)
    content = models.CharField(max_length=30, default=‘‘, verbose_name=‘内容‘, help_text=‘最多30字‘)
    is_read=models.BooleanField(default=False,verbose_name=‘是否已读‘)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name=‘评论表‘
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.content

class Pick(models.Model):
    """
    点赞表
    """
    user = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name=‘点赞者‘)
    wish = models.ForeignKey(Wish, on_delete=models.CASCADE, verbose_name=‘被点赞的愿望‘)
    action=models.BooleanField(default=True,verbose_name=‘行为‘,help_text=‘true是点赞false是取消点赞‘)

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name=‘点赞表‘
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.user

class Share(models.Model):
    """
    分享表
    """
    user = models.ForeignKey(WxUsers, on_delete=models.CASCADE, verbose_name=‘分享者‘)
    wish = models.ForeignKey(Wish, on_delete=models.CASCADE, verbose_name=‘被分享的愿望‘)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name=‘分享表‘
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.user

class Notice(models.Model):
    """
    系统通知表(超越私信)
    """
    admin=models.ForeignKey(UserProfile,on_delete=models.CASCADE)
    content = models.CharField(max_length=200, default=‘‘, verbose_name=‘内容‘, help_text=‘最多200字‘)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name=‘系统通知表‘
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.content

2.执行数据更新命令:

python manage.py makemigrations
python manage.py migrate

2.引入xadmin

1.将xadmin文件夹放入extra_apps目录下:

2.在每个app下新建adminx.py

1.apps/users/目录下新建adminx.py:

import xadmin
from xadmin import views
from .models import WxUsers

class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True

class GlobalSettings(object):
    site_title = "超越村后台管理系统"
    site_footer = "chaoyuecun"
    # menu_style = "accordion"

class WxUsersAdmin(object):
    list_display = ["open_id", "token", "nickName", "gender","province","city","country","last_login_time","add_time"]
    list_filter = [ "gender","province","city","country","last_login_time","add_time"]
    search_fields = ["gender","province","city","country" ]

xadmin.site.register(WxUsers, WxUsersAdmin)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)

2.apps/wish/目录下新建adminx.py:

import xadmin
from .models import Wish, News

class WishAdmin(object):
    list_display = [‘wxuser‘, ‘content‘,"status","pv_num","up_num", "share_num","bb_num","end_time","add_time"]
    list_filter =[‘wxuser‘, ‘content‘,"status","pv_num","up_num", "share_num","bb_num","end_time","add_time"]
    search_fields=[‘wxuser‘, ‘content‘,"status","pv_num","up_num", "share_num","bb_num"]

class NewsAdmin(object):
    list_display = [‘user‘, ‘banner‘,"title","content","add_time"]
    list_filter =[‘user‘, ‘banner‘,"title","content","add_time"]
    search_fields=[‘user‘, ‘banner‘,"title","content"]

xadmin.site.register(Wish, WishAdmin)
xadmin.site.register(News, NewsAdmin)

3.apps/user_operation/目录下新建adminx.py:

import xadmin
from .models import Message,Pick,Share,Notice

class MessageAdmin(object):
    list_display = [‘user_send‘, ‘user_receive‘,"wish","content","is_read","add_time"]
    list_filter =[‘user_send‘, ‘user_receive‘,"wish","content","is_read","add_time"]
    search_fields=[‘user_send‘, ‘user_receive‘,"wish","content","is_read"]

class PickAdmin(object):
    list_display = [‘user‘, ‘wish‘,"action","add_time"]
    list_filter =[‘user‘, ‘wish‘,"action","add_time"]
    search_fields=[‘user‘, ‘wish‘]

class ShareAdmin(object):
    list_display = [‘user‘, ‘wish‘,"add_time"]
    list_filter =[‘user‘, ‘wish‘,"add_time"]
    search_fields=[‘user‘, ‘wish‘]

class NoticeAdmin(object):
    list_display = [‘admin‘, ‘content‘,"add_time"]
    list_filter =[‘admin‘, ‘content‘,"add_time"]
    search_fields=[‘admin‘, ‘content‘]

xadmin.site.register(Message, MessageAdmin)
xadmin.site.register(Pick, PickAdmin)
xadmin.site.register(Share, ShareAdmin)
xadmin.site.register(Notice, NoticeAdmin)

3.安装和配置xadmin依赖包

1.安装依赖包 django-crispy-forms django-reversion django-formtools future httplib2 six django-import-export

pip install django-crispy-forms django-reversion django-formtools future httplib2 six django-import-export

2.在settings中注册

INSTALLED_APPS = [
    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
    ‘users.apps.UsersConfig‘,
    ‘wish.apps.WishConfig‘,
    ‘user_operation.apps.UserOperationConfig‘,
    ‘crispy_forms‘,
    ‘xadmin‘
]

3.安装用来操作和导出Excel文件的包 xlwt xlsxwriter

pip install xlwt xlsxwriter

4.打开终端,执行数据更新命令,生成xadmin默认的表

python manage.py makemigrations
python manage.py migrate

5.配置url,在chaoyuecun/urls.py:

from django.contrib import admin
from django.urls import path
import xadmin

urlpatterns = [
    # path(‘admin/‘, admin.site.urls),
    path(‘xadmin/‘, xadmin.site.urls),
]

6.创建超级用户

python manage.py createsuperuser
Username: admin
邮箱:
Password:
Password (again):

创建完毕,启动项目,然后访问

http://127.0.0.1:8000/xadmin/

就可以登录xadmin后台了,但是此时后台还是英文的。

7.将语言设定为中文

1.在settings中修改

LANGUAGE_CODE = ‘zh-hans‘

TIME_ZONE = ‘Asia/Shanghai‘

USE_I18N = True

USE_L10N = True

USE_TZ = False

2.在每个app下的apps.py中做修改,加上verbose_name

1.apps/users/apps.py:

from django.apps import AppConfig

class UsersConfig(AppConfig):
    name = ‘users‘
    verbose_name = ‘用户‘

1.apps/user_operation/apps.py:

from django.apps import AppConfig

class UserOperationConfig(AppConfig):
    name = ‘user_operation‘
    verbose_name = ‘用户操作‘

1.apps/wish/apps.py:

from django.apps import AppConfig

class WishConfig(AppConfig):
    name = ‘wish‘
    verbose_name = ‘愿望‘

这时再看xadmin界面,已经变成中文的了。

原文地址:https://www.cnblogs.com/xuepangzi/p/10582946.html

时间: 2024-10-05 05:50:24

超越村后端开发(2:新建models.py+xadmin的引入)的相关文章

超越村后端开发(5:远程同步本地与服务器端的MySQL数据库)

1.同步MySQL数据库 服务器选用的华为云,安装了Ubuntu18.04,华为云默认是以root用户登录的. 1.使用Xshell6连接华为云 ls 2.Ubuntu18.04安装MySQL5.7 1.安装命令: apt-get install mysql-server y apt-get install mysql-client apt-get install libmysqlclient-dev y 2.配置密码: 1.进入mysql 安装MySQL5.7的过程中是没有让用户输入初始密码这

广告小程序后端开发(2.Models设计)

1.users的数据表设计: 1.重建用户表: 1.在settings中配置: AUTH_USER_MODEL='users.UserProfile' 2.apps/users/models.py中建立UserProfile表: class UserProfile(AbstractUser): """ 用户表 """ nickName=models.CharField(max_length=30,null=True,blank=True,verb

广告小程序后端开发(5.安装配置django-rest-framework,编写后台登录逻辑)

1.安装djangorestframework 1.安装djangorestframework及其依赖包markdown.django-filter. pip install djangorestframework markdown django-filter 2.在settings中注册,如下: INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'dj

广告小程序后端开发(6.获取类别逻辑)

1.获取类别逻辑: 1.在apps/ad目录下新建serializers.py文件进行序列化: from rest_framework import serializers from .models import Category,Good,Prize,Images class CategoryModelSerializer(serializers.ModelSerializer): class Meta: model = Category fields="__all__" class

广告小程序后端开发(7.获取价格表数据)

1.在xadmin后台,手动添加数据: http://127.0.0.1:8000/xadmin/trade/price/ 2.在apps/trade/目录下新建序列化文件serializers.py: from rest_framework import serializers from .models import Price,Order,ShoppingCar class PriceModelSerializer(serializers.ModelSerializer): class Me

广告小程序后端开发(16.优惠券系统:原理流程图,奖品实验数据,生成二维码)

业务需求:在一个广告小程序内发许多其他的小程序的优惠券. 1.原理流程图 2.奖品(优惠券)实验数据 1.修改apps/ad/models.py中的Prize表类 class Prize(models.Model): """奖品""" title = models.CharField(max_length=15, null=True, blank=True, verbose_name='标题', help_text='最多15字') url=mo

Django模型层(models.py)之模型创建

Django数据库操作是十分重要的内容,这两天简单学习了数据库的操作,这里做个总结. 1.ORM简介 简单的来说,ORM就是对象-关系-映射.它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库. 2.创建模型前的准备 在settings中配置databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'myorm_test', #需要连

后端开发使用pycharm的技巧

目录 后端开发使用pycharm的技巧 1.使用说明 2.database 3.HTTP Client 1.使用说明 首先说明,本文所使用的功能为pycharm专业版所支持,其他版本如社区版,教育版,则不一定支持. 作为一名后端开发,我猜你的桌面上一定打开着一系列的软件,用作开发调试工具,比如navicat数据库连接工具,postman接口调试工具,pycharm代码编写IDE,以及其他一些工具.今天,我就介绍一下pycharm中你可能还不知道的一些功能,让你的IDE.postman.navic

web前端开发和后端开发有什么区别?

web前端分为网页设计师.网页美工.web前端开发工程师 首先网页设计师是对网页的架构.色彩以及网站的整体页面代码负责 网页美工只针对UI这块儿的东西,比如网站是否做的漂亮 web前端开发工程师是负责交互设计的,需要和程序猿进行交互设计的配合. web前端需要掌握的有脚本技术javascript DIV+CSS现下最流行的页面搭建技术,ajax和jquery以及简单的后端程序等. 后端的话可供开发的语言有 asp.php.jsp..NET 这些后端开发语言的话搭建环境都不一样,具体如果你想学的话