• Python学习---抽屉框架分析[ORM操作]180314


    Django ORM操作


        1. 字段操作
             class User(model.Model);
                 u=字段
            用处:
                1 .admin中的字段验证
                2. obj.clean_fields() 进行自定义的验证
                3. 利用Djanfo Form进行验证,此时前台和后台的操作分开
                   但form和model里的字段重复【推荐使用】
                        ModelForm组件验证
                          name
                          email
                       model组件操作数据
                           name
                           email
                4. 利用ModelForm实现models的字段验证【Admin就是基于此验证的】
                   2个文件直接的依赖性太强,适合小项目【不推荐使用】
                       ModelForm组件验证
                          直接采用model里面的字段
                       model组件操作数据
                           name
                           email
        2. 内容操作

    案例操作

    字段用处1. admin中的字段验证,仅仅用于单表

    settings.py

    INSTALLED_APPS = [
       ...
     'app01',   # 注册app
    ]
    STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),)  # 现添加的配置,这里是元组,注意逗号
    TEMPLATES = [
       ...
       'DIRS': [os.path.join(BASE_DIR, 'templates')],
    ]

    admin.py

    from django.contrib import admin
    from app01 import models
    # Register your models here.
    
    # 添加信息到Django Admin
    admin.site.register(models.Test)

    urls.py

    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import url, include
    from app01 import views
    urlpatterns = [
       url('admin/', admin.site.urls),
    ]

    views.py

    from django.shortcuts import render, redirect
    from app01 import models
    

    页面显示;

    image

    初始化数据库

    python manage.py makemigrations
    python manage.py migrate
    

    创建admin用户

    python manage.py createsuperuser

    字段用处2. obj.clean_fields() 进行自定义的验证

    settings.py

    INSTALLED_APPS = [
       ...
     'app01',   # 注册app
    ]
    STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),)  # 现添加的配置,这里是元组,注意逗号
    TEMPLATES = [
       ...
       'DIRS': [os.path.join(BASE_DIR, 'templates')],
    ]

    urls.py

    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import url, include
    from app01 import views
    urlpatterns = [
       # 测试
     url(r'^test.html', views.test),
    ]

    models.py

    from django.db import models
    class Test(models.Model):
        username = models.CharField(max_length=32, unique=True)
        email = models.EmailField()

    models.py

    from django.db import models
    class Test(models.Model):
        username = models.CharField(max_length=32, unique=True)
        email = models.EmailField()

    views.py

    from django.shortcuts import render, redirect
    from app01 import models
    # 测试
    def test(request):
        # 实例化一个对象
        obj = models.Test(username='root', email='hhhlive.com')
        # model进行参数的校验
        obj.clean_fields()
        obj.save()  # 数据进行保存
        return HttpResponse("test")

    页面显示;

    image

    用处4: 基于ModelForm实现Django Admin字段添加[不更改数据库]

    settings.py

    INSTALLED_APPS = [
       ...
     'app01',   # 注册app
    ]
    STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),)  # 现添加的配置,这里是元组,注意逗号
    TEMPLATES = [
       ...
       'DIRS': [os.path.join(BASE_DIR, 'templates')],
    ]

    urls.py

    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import url, include
    from app01 import views
    urlpatterns = [
       url('admin/', admin.site.urls),
    ]

    model.py

    from django.db import models
    class Test(models.Model):
        username = models.CharField(max_length=32, unique=True)
        email = models.EmailField()

    admin.py

    from app01 import models
    from  django import forms
    from django.contrib import admin
    class TestForm(forms.ModelForm):  # ModelForm和Form一样均继承了Model
        username = forms.CharField(error_messages={'required':'用户名不能为空'})
        email = forms.EmailField(error_messages={'invalid': '邮箱格式错误'})
        age = forms.IntegerField(error_messages={"required": '请输入数字'})
        class Meta:
            models = models.Test
            # fields = ('username',)
            fields = '__all__'    # 将model里面Test的全部字段倒给TestForm,此时具有验证功能
    # 添加信息到Django Admin
    # 此时Django Admin 里面有5个字段, Test的2个 + TestForm的3个
    admin.site.register(models.Test, TestForm)
  • 相关阅读:
    Windows性能调优: Perfomn.exe 和Perfmon /res
    WPF:逻辑树和视觉树
    AD FS 概述
    SQL Server : TRUSTWORTHY 数据库属性
    WCF:在开发期间应该注意的问题
    ASP.NET MVC 2中的数据验证
    SQL Server:如何在Service Broker发送消息验证失败后获取源消息
    GDI+:自定义控件时如何使用Region来输出特定区域
    LINQ to XML:如何替换XCData的内容
    javascript是否真的就不能实现跨站请求呢?
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9410861.html
Copyright © 2020-2023  润新知