zoukankan      html  css  js  c++  java
  • Django—models相关操作

    一、在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))
  • 相关阅读:
    【荐】说说CSS Hack 和向后兼容
    【阮一峰】深入研究URL编码问题及JavaScript相应的解决方案
    什么是H标签?H1,H2,H3标签?以及和strong标签使用的方法及重要性
    实用框架(iframe)代码
    数据库(SQLITE3函数总结): sqlite3_open, sqlite3_exec, slite3_close,sqlite3_prepare_v2,sqlite3_column_text,
    BZOJ 3110 ZJOI 2013 K大数查询 树套树(权值线段树套区间线段树)
    c++中基本的语法问题
    RIP协议两个版本号对不连续子网的支持情况实验
    getChars的使用方法
    ios8中百度推送接收不到
  • 原文地址:https://www.cnblogs.com/wangwei916797941/p/6923940.html
Copyright © 2011-2022 走看看