码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-23 13:19:04      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:邮箱   enable   xls   ann   reg   image   rac   open   delete   

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
View Code

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 = 图片
View Code

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
View Code

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)
View Code

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)
View Code

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)
View Code

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界面,已经变成中文的了。

技术图片

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

标签:邮箱   enable   xls   ann   reg   image   rac   open   delete   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!