[django]Manipulator解惑

代码下载:/Files/maplye/django/Study914.rar

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等等。
如:
None.gif from  django  import  forms
None.gif
None.gifurgency_choices 
=  (
None.gif    (
1 " Extremely urgent " ),
None.gif    (
2 " Urgent " ),
None.gif    (
3 " Normal " ),
None.gif    (
4 " Unimportant " ),
None.gif)
None.gif
None.gif
class  ContactManipulator(forms.Manipulator):
None.gif    
def   __init__ (self):
None.gif        self.fields 
=  (
None.gif            forms.EmailField(field_name
= " from " , is_required = True),
None.gif            forms.TextField(field_name
= " subject " , length = 30 , maxlength = 200 , is_required = True),
None.gif            forms.SelectField(field_name
= " urgency " , choices = urgency_choices),
None.gif            forms.LargeTextField(field_name
= " contents " , is_required = True),
None.gif        )
None.gif    
def  save(self):         # dot.gifdot.gif.

而且我们还可以定义带有初始化参数的custom Manipulator
如:
None.gif class  ContactManipulator(forms.Manipulator):
None.gif    
def   __init__ (self,object_id = None):
None.gif        self.object_id 
=  object_id
None.gif        self.fields 
=  (
None.gif            forms.EmailField(field_name
= " fromEmail " , is_required = True),
None.gif            forms.TextField(field_name
= " subject " , length = 30 , maxlength = 200 , is_required = True),
None.gif            forms.SelectField(field_name
= " urgency " , choices = urgency_choices),
None.gif            forms.LargeTextField(field_name
= " contents " , is_required = True),
None.gif        )
None.gif
None.gif    
def  save(self, new_data):
None.gif        fromemail 
=  new_data[ " fromEmail " ]
None.gif        subject 
=  new_data[ " subject " ]
None.gif        urgency 
=  new_data[ " urgency " ]
None.gif        contents 
=  new_data[ " contents " ]
None.gif        
if  self.object_id:
None.gif            obj 
=  Contact.objects.get(pk = int(self.object_id))
None.gif            obj.fromEmail 
=  fromemail
None.gif            obj.subject 
=  subject
None.gif            obj.urgency 
=  urgency;
None.gif            obj.contents 
=  contents;
None.gif        
else :
None.gif            obj 
=  Contact(fromEmail = fromemail, subject = subject, urgency = urgency,contents = contents)
None.gif        obj.save()
你看上面ContactManipulator的Manipulator就具有AddManipulator和ChangeManipulator两个的功能,
那这个ContactManipulator应该怎么用呢
None.gif # 创建   
None.gif
def  contact_form(request):
None.gif    manipulator 
=  ContactManipulator()
None.gif    
if  request.POST:
None.gif        new_data 
=  request.POST.copy()
None.gif        errors 
=  manipulator.get_validation_errors(new_data)
None.gif        
if   not  errors:
None.gif            manipulator.do_html2python(new_data)
None.gif
None.gif            manipulator.save(new_data)
None.gif            
return  HttpResponseRedirect( " /formapp/contact/ " )
None.gif    
else :
None.gif        errors 
=  new_data  =  {}
None.gif        
None.gif    form 
=  forms.FormWrapper(manipulator, new_data, errors)
None.gif    
return  render_to_response( ' places/contact_form.html ' , { ' form ' : form})
None.gif
None.gif
# 修改
None.gif
def  editcontact(request,contactid):
None.gif    manipulator 
=  ContactManipulator(contactid)
None.gif    
None.gif    
if  request.POST:
None.gif        new_data 
=  request.POST.copy()
None.gif        errors 
=  manipulator.get_validation_errors(new_data)
None.gif        
if   not  errors:
None.gif            manipulator.do_html2python(new_data)
None.gif            manipulator.save(new_data)
None.gif
None.gif            
#  Do a post-after-redirect so that reload works, etc.
None.gif
             return  HttpResponseRedirect( " /formapp/contact/1 " )
None.gif    
else :
None.gif        errors 
=  {}        
None.gif        contact 
=  Contact.objects.get(id  =  contactid)
None.gif        new_data 
=  contact. __dict__
None.gif    
None.gif    form 
=  forms.FormWrapper(manipulator, new_data,errors )
None.gif    
return  render_to_response( ' places/contact_form.html ' , { ' form ' : form})        
None.gif

以上是我在学习Manipulator时一些个人理解,暂时记录如此,以后我会更深入的了解他们,同时非常感谢limodou的解惑,他真是个热心人,向他学习!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值