[Django] Manipulator

Source: Internet
Author: User
Code Download:/files/maplye/Django/study914.rar

Manipulator can be used to create, modify, and verify an object. In combination with formwrapper, You can automatically generate HTML code on the page: formwrapper is used to generate HTML code. manipulator provides data and data models for formwrapper ). The Django system already has two types of manipulator. One is Addmanipulator. The second isChangemanipulator, I used to think that this is two methods of the model.Changemanipulator I thought it was to replace a manipulator for my model,Actually, it is two different classes. The two classes can automatically generate validation and save based on the model.Addmanipulator andChangemanipulator is inherited fromManipulator, the difference isChangemanipulator carries the objectid constructor, obtains the original_object attribute assigned to an object, modifies the object in the SAVE method, and saves the object. For the use of the two manipulator, refer to Django document: Http://www.djangoproject.com/documentation/forms/ In addition to the two types of manipulator provided by Django, we can also customize manipulator so that we can define our own fields, validation, and save. For example: From Django Import Forms

Urgency_choices = (
( 1 , " Extremely urgent " ),
( 2 , " Urgent " ),
( 3 , " Normal " ),
( 4 , " Unimportant " ),
)

Class Contactmanipulator (forms. Manipulator ):
Def   _ Init __ (Self ):
Self. Fields = (
Forms. emailfield (field_name = " From " , Is_required = True ),
Forms. textfield (field_name = " Subject " , Length = 30 , Maxlength = 200 , Is_required = True ),
Forms. selectfield (field_name = " Urgency " , Choices = Urgency_choices ),
Forms. largetextfield (field_name = " Contents " , Is_required = True ),
)
Def Save (Self ): # .

We can also define the M Manipulator with initialization parameters.
For example:Class Contactmanipulator (forms. Manipulator ):
Def   _ Init __ (Self, object_id = None ):
Self. object_id = Object_id
Self. Fields = (
Forms. emailfield (field_name = " Fromemail " , Is_required = True ),
Forms. textfield (field_name = " Subject " , Length = 30 , Maxlength = 200 , Is_required = True ),
Forms. selectfield (field_name = " Urgency " , Choices = Urgency_choices ),
Forms. largetextfield (field_name = " Contents " , Is_required = True ),
)

Def Save (self, new_data ):
Fromemail = New_data [ " Fromemail " ]
Subject = New_data [ " Subject " ]
Urgency = New_data [ " Urgency " ]
Contents = New_data [ " Contents " ]
If Self. object_id:
OBJ = Contact. Objects. Get (pk = INT (self. object_id ))
OBJ. fromemail = Fromemail
OBJ. Subject = Subject
OBJ. Urgency = Urgency;
OBJ. Contents = Contents;
Else :
OBJ = Contact (fromemail = Fromemail, Subject = Subject, urgency = Urgency, Contents = Contents)
OBJ. Save ()

You can see that the manipulator of contactmanipulator has two functions: addmanipulator and changemanipulator,
How Should contactmanipulator be used? # Create
Def Contact_form (request ):
Manipulator = Contactmanipulator ()
If Request. Post:
New_data = Request. Post. Copy ()
Errors = Manipulator. get_validation_errors (new_data)
If   Not Errors:
Manipulator. do_html2python (new_data)

Manipulator. Save (new_data)
Return Httpresponseredirect ( " /Formapp/contact/ " )
Else :
Errors = New_data = {}

Form = Forms. formwrapper (manipulator, new_data, errors)
Return Render_to_response ( ' Places/contact_form.html ' ,{ ' Form ' : Form })

# Modify
Def Editcontact (request, contactid ):
Manipulator = Contactmanipulator (contactid)

If Request. Post:
New_data = Request. Post. Copy ()
Errors = Manipulator. get_validation_errors (new_data)
If   Not Errors:
Manipulator. do_html2python (new_data)
Manipulator. Save (new_data)

# Do a post-after-redirect so that reload works, etc.
Return Httpresponseredirect ( " /Formapp/contact/1 " )
Else :
Errors = {}
Contact = Contact. Objects. Get (ID = Contactid)
New_data = Contact. _ Dict __

Form = Forms. formwrapper (manipulator, new_data, errors)
Return Render_to_response ( ' Places/contact_form.html ' ,{ ' Form ' : Form })

The above are some of my personal understandings when I was learning manipulator. I keep a record for the time being. I will learn more about them in the future. I am also very grateful to limodou for his questions. He is a really enthusiastic person and I want to learn from him!

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.