_潜行者

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、在django后台admin管理页面添加自己增加的表结构

  通过终端命令:python3 manage.py makemigrations, python3 manage.py migrate

  我们在Django自带的后台管理页面可以看到Django自动生成表结构,那么要是想成自己的表结构需要这么做:

  首先web应用的models.py文件里面配置自己的数据库表,例如下面要建一张UserInfo表,并且用UserInfoAdmin指定后台表详情 :

   

class UserInfo(models.Model):
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
email = models.CharField(max_length=50)
userType = models.ForeignKey(UserType)#在上面声明过,所有可以直接写不加引号

class Meta:
verbose_name = '用户info'
verbose_name_plural = verbose_name

# def __unicode__(self):#python2中使用
def __str__(self): # 这个方法类比打印对象一样自定义打印,list_display = ('name', 'age')会覆盖此方法效果
return '[username: %s, password: %s, email: %s]'%(self.username, self.password, self.email)

class UserInfoAdmin(admin.ModelAdmin):
list_display = ('username', 'password', 'email')
search_fields = ('username', 'email')
list_display_links = ('password',)# which cannot be used unless 'list_display_links' is set.
list_editable = ('username','email',)

  写完之后我们只需要在admin.py里面注册就好了

from django.contrib import admin

# Register your models here.
from app01 import models


admin.site.register(models.UserInfo, models.UserInfoAdmin) 

# admin.site.register(models.UserGroup)
#
admin.site.register(models.Asset)

二、models相关操作

  1,增

    obj = models.UserInfo(username = 'xixi', password = '123', email = '123@kd.ocm', userType = models.UserType.objects.get(id = 2))
    obj.save()

    obj = models.UserInfo(username='xixi', password='123', email='123@kd.ocm', userType_id=2)#注意此处是一个下划线
    obj.save()

    models.UserInfo.objects.create(username='gan', password='123', email='123@kd.ocm', userType_id=1)
    # TypeError: 'userType_name' is an invalid keyword argument for this function

    info_dict = {'username':'wei', 'password':'123', 'email':'123@kd.ocm', 'userType_id':1}#注意此处是一个下划线
    models.UserInfo.objects.create(**info_dict)

  2,删

    def delNewsByID(self, id):
        models.News.objects.filter(nid=id).delete()
    def delNewsByTitle(self, id):
        models.News.objects.filter(title=id).delete()

  3,改

    def updateNewsByID(self, id, title, content):
        models.News.objects.filter(nid= id).update(title = title, content = content)
        # news = models.News.objects.get(nid=id)
        # news.title = 'updatetitle'
        # news.save()

 

  4,查

    ref = models.UserInfo.objects.all().values('username', 'email')
    print(ref, '----', ref.query)
    # < QuerySet[{'username': 'wang', 'email': '123@qw.com'}, {'username': 'xixi', 'email': '123@kd.ocm'}, {'username': 'xixi', 'email': '123@kd.ocm'},
    # { 'username': 'gan', 'email': '123@kd.ocm'}] > ---- SELECT`app01_userinfo`.`username`, `app01_userinfo`. `email` FROM `app01_userinfo`

    # ref = models.UserInfo.objects.all().values('username', 'userType__id', 'userType__name', 'userType__userinfo__username')#注意此处是2个下划线
    ref = models.UserInfo.objects.filter(userType__name='administrator').values('username', 'userType__id', 'userType__name')
    print(ref, '----', ref.query)
    # < QuerySet[{'username': 'wang', 'userType__id': 1, 'userType__name': 'administrator'}, {'username': 'xixi', 'userType__id': 2, 'userType__name': 'user'}, { 'username': 'xixi', 'userType__id': 2, 'userType__name': 'user'}, {'username': 'gan', 'userType__id': 1, 'userType__name': 'administrator'}, { 'username': 'wei', 'userType__id': 1, 'userType__name': 'administrator'},
    # {'username': 'wei', 'userType__id': 1, 'userType__name': 'administrator'}] > ---- SELECT `app01_userinfo`. `username`, `app01_userinfo`.
    # `userType_id`, `app01_usertype`. `name` FROM `app01_userinfo` INNER  JOIN `app01_usertype` ON(`app01_userinfo`. `userType_id` = `app01_usertype`.`id`)
    for item in ref:
        print(item, '==', type(item))

 

posted on 2017-05-31 14:19  _潜行者  阅读(487)  评论(0编辑  收藏  举报