Python Learning---drawer frame analysis [ORM Operation]180314

Source: Internet
Author: User

Django ORM Operations


1. Field Operations
Class User (model. Model);
u= Field
Use:
1. Field validation in admin
2. Obj.clean_fields () for custom validation
3. Use Djanfo form for verification, at which time the foreground and background operations are separated
But the fields in the form and model repeat the "recommended use"
Modelform Component Validation
Name
Email
Model Component Operation data
Name
Email
4. Using Modelform to implement models field validation "Admin is based on this verification"
2 files direct dependencies are too strong, suitable for small projects "deprecated"
Modelform Component Validation
Directly using the field in model
Model Component Operation data
Name
Email
2. Content Operation

Case actions

field use 1. Field validation in admin, only for single-table

settings.py

Installed_apps = [   ... ' App01 ',   # Register App]staticfiles_dirs = (Os.path.join (base_dir, "statics"),  # Now add the configuration, here is the tuple, note the comma templates = [   ...   ' DIRS ': [Os.path.join (Base_dir, ' templates ')],]

admin.py

From Django.contrib import adminfrom app01 import models# Register Your models here.# add information to Django Adminadmin.site.register (Models. Test)

urls.py

From Django.contrib import adminfrom django.urls import pathfrom django.conf.urls import URL, includefrom app01 import vie Wsurlpatterns = [   url (' admin/', admin.site.urls),]

views.py

From django.shortcuts import Render, Redirectfrom APP01 import Models

page display;

Initializing the database

Python manage.py Makemigrationspython manage.py Migrate

Create Admin user

Python manage.py Createsuperuser

Field Use 2. obj.clean_fields () for custom validation

settings.py

Installed_apps = [   ... ' App01 ',   # Register App]staticfiles_dirs = (Os.path.join (base_dir, "statics"),  # Now add the configuration, here is the tuple, note the comma templates = [   ...   ' DIRS ': [Os.path.join (Base_dir, ' templates ')],]

urls.py

From Django.contrib import adminfrom django.urls import pathfrom django.conf.urls import URL, includefrom app01 import vie Wsurlpatterns = [   # test URL (R ' ^test.html ', views.test),]

models.py

From django.db import Modelsclass Test (models. Model):    username = models. Charfield (max_length=32, unique=true)    email = models. Emailfield ()

models.py

From django.db import Modelsclass Test (models. Model):    username = models. Charfield (max_length=32, unique=true)    email = models. Emailfield ()

views.py

From django.shortcuts import Render, Redirectfrom APP01 import models# Testing def test (Request):    # instantiating an object    obj = Models . Test (username= ' root ', email= ' hhhlive.com ')    # model parameters Check    obj.clean_fields ()    obj.save ()  # Data to save    return HttpResponse ("test")

page display;

Use 4: Implement the Django admin field based on Modelform [Do not change the database]

settings.py

Installed_apps = [   ... ' App01 ',   # Register App]staticfiles_dirs = (Os.path.join (base_dir, "statics"),  # Now add the configuration, here is the tuple, note the comma templates = [   ...   ' DIRS ': [Os.path.join (Base_dir, ' templates ')],]

urls.py

From Django.contrib import adminfrom django.urls import pathfrom django.conf.urls import URL, includefrom app01 import vie Wsurlpatterns = [   url (' admin/', admin.site.urls),]

model.py

From django.db import Modelsclass Test (models. Model):    username = models. Charfield (max_length=32, unique=true)    email = models. Emailfield ()

admin.py

From APP01 import modelsfrom  Django import formsfrom django.contrib import Adminclass testform (forms. Modelform):  # Modelform and form inherit the model    username = forms. Charfield (error_messages={' Required ': ' User name cannot be empty '})    email = forms. Emailfield (error_messages={' invalid ': ' Mailbox format error '}) Age    = forms. Integerfield (error_messages={"required": ' Please enter a number '})    class Meta:        models = models. Test        # field = (' username ',)        fields = ' __all__ '    # to Testform, with validation function # Add information to Django Admin # at this time Django Admin has 5 fields, test 2 + testform 3 Admin.site.register (models. Test, Testform)

Python Learning---drawer frame analysis [ORM Operation]180314

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.