Python学习---抽屉框架分析[ORM操作]180314
Posted 小a玖拾柒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
页面显示;
初始化数据库
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")
页面显示;
用处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)
以上是关于Python学习---抽屉框架分析[ORM操作]180314的主要内容,如果未能解决你的问题,请参考以下文章
Python学习---抽屉框架分析[数据库设计分析]180313
Python学习---抽屉框架分析[点赞功能/文件上传分析]0317