代码下载:/Files/maplye/django/Study914.rar
Manipulator英语翻译成“操纵器”,你可以用它来创建,修改,验证你的一个实体对象,并结合FormWrapper可以自动生成页面上Html代码(注:FormWrapper是用来生成html代码,Manipulator使为FormWrapper提供数据和数据模型等)。
Manipulator英语翻译成“操纵器”,你可以用它来创建,修改,验证你的一个实体对象,并结合FormWrapper可以自动生成页面上Html代码(注:FormWrapper是用来生成html代码,Manipulator使为FormWrapper提供数据和数据模型等)。
django系统已经带有两种Manipulator,一是AddManipulator,二是ChangeManipulator,我以前一直以为这是model的两个方法,还有ChangeManipulator我以为是为我的model更换一个Manipulator,其实他是两个不同的类。这两个类能够自动根据model来生成validation和save。AddManipulator和ChangeManipulator都是继承自Manipulator,不同的是ChangeManipulator带有一objectid的构造参数,并获取一object赋值给他的original_object属性,并在save方法使用修改对象,然后保存该对象。
关于这两种Manipulator的使用,可以参考django document:
除了django提供的两种Manipulator,我们还可以自定义Manipulator,这样我们定义自己的fields,validation,save等等。
如:
而且我们还可以定义带有初始化参数的custom Manipulator
如:
那这个ContactManipulator应该怎么用呢
以上是我在学习Manipulator时一些个人理解,暂时记录如此,以后我会更深入的了解他们,同时非常感谢limodou的解惑,他真是个热心人,向他学习!
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): # .
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): # .
而且我们还可以定义带有初始化参数的custom Manipulator
如:
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()
你看上面ContactManipulator的Manipulator就具有AddManipulator和ChangeManipulator两个的功能,
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()
那这个ContactManipulator应该怎么用呢
#
创建
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})
# 修改
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})
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})
# 修改
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})
以上是我在学习Manipulator时一些个人理解,暂时记录如此,以后我会更深入的了解他们,同时非常感谢limodou的解惑,他真是个热心人,向他学习!