2013-04-09 83 views
1

我有一個工作的保存方法在我的形式,一旦我升級是演戲很奇怪的,我似乎無法調試問題的根源Django的形式保存方法升級,從1.4到1.5

我有一個簡單的表格從繼承的ModelForm內我已經推翻了保存方法來保存一些外部atteibutes

下面是我的代碼

class UserProfileForm(ExtendedMetaModelForm): 
    """ 
    UserProfileForm 

    """ 
    _genders = (
     ('M', _('Male')), 
     ('F', _('Female')), 
     ) 

    birthday   = forms.DateField(
     widget=extras.SelectDateWidget(attrs={'class' : 'span1'},years=(range(1930, datetime.now().year-14))), 
     label = _('Birthday'), 
     required= False, 
     error_messages = { 
      'required' : _('Birthday is required.') 
     } 

    ) 
    gender   = forms.CharField(
     label = _('Gender'), 
     widget = forms.Select(choices=_genders) 
    ) 
    bio    = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows' : '4'})) 

    class Meta: 
     model = User 
     fields = ('first_name', 'last_name', 'bio', 'birthday', 'gender', 'email',) 



    def __init__(self, *args, **kwargs): 
     super(UserProfileForm, self).__init__(*args, **kwargs) 



     if self.instance: 
      self.fields['email'].widget.attrs['readonly'] = True 
      self.fields['birthday'].initial = self.instance.get_profile().birthday 
      self.fields['bio'].initial = self.instance.get_profile().bio 

     for i in self.fields: 
      if isinstance(self.fields[i], forms.CharField): 
       self.fields[i].widget.attrs["class"]  = 'input-xlarge' 

    def save(self, *args, **kw): 
     super(UserProfileForm, self).save(*args, **kw) 
     self.instance.get_profile().bio   = self.cleaned_data.get('bio') 
     self.instance.get_profile().birthday  = self.cleaned_data.get('birthday') 
     self.instance.get_profile().save() 

上述工作正常,現在。在初始化表單時,它將從配置文件中檢索生物,生日的初始值。

但是,當保存它時沒有采取任何行動。我的個人資料模型是非常基本的,並沒有改變它的保存方法,它使用模型的原始操作。模型

任何人都可以建議爲什麼發生這種情況?

PS不返回錯誤,它只是不保存任何

更新(添加ExtendedMetaModelForm類):

class ExtendedMetaModelForm(forms.ModelForm): 
    """ 
    Allow the setting of any field attributes via the Meta class. 
    """ 
    def __init__(self, *args, **kwargs): 
     """ 
     Iterate over fields, set attributes from Meta.field_args. 
     """ 
     super(ExtendedMetaModelForm, self).__init__(*args, **kwargs) 
     if hasattr(self.Meta, "field_args"): 
      # Look at the field_args Meta class attribute to get 
      # any (additional) attributes we should set for a field. 
      field_args = self.Meta.field_args 
      # Iterate over all fields... 
      for fname, field in self.fields.items(): 
       # Check if we have something for that field in field_args 
       fargs = field_args.get(fname) 
       if fargs: 
        # Iterate over all attributes for a field that we 
        # have specified in field_args 
        for attr_name, attr_val in fargs.items(): 
         if attr_name.startswith("+"): 
          merge_attempt = True 
          attr_name = attr_name[1:] 
         else: 
          merge_attempt = False 
         orig_attr_val = getattr(field, attr_name, None) 
         if orig_attr_val and merge_attempt and\ 
          type(orig_attr_val) == dict and\ 
          type(attr_val) == dict: 
          # Merge dictionaries together 
          orig_attr_val.update(attr_val) 
         else: 
          # Replace existing attribute 
          setattr(field, attr_name, attr_val) 
+0

什麼是'ExtendedMetaModelForm'?它是否超過'save'? – danodonovan 2013-04-09 15:54:33

+0

ExtendedMetaModelForm我用來創建一個可配置的小部件。查找ExtendedMetaModelForm的上述代碼,我剛更新了這個問題 – 2013-04-09 16:05:28

回答

2

正如@Ngenator指出的那樣,您正在使用不推薦使用的函數。你可以嘗試creating a customUser模型

settings.py

AUTH_USER_MODEL = 'myapp.MyUser' 

myapp.MyUser創建具有屬性的新用戶你指定

class MyUser(AbstractBaseUser): 

    bio = TextField() 
    birthday = DateField() 

,你會需要從擺脫get_profile()你的表格。

def save(self, *args, **kw): 

    self.instance.bio   = self.cleaned_data.get('bio') 
    self.instance.birthday  = self.cleaned_data.get('birthday') 

    super(UserProfileForm, self).save(*args, **kw) 

注 - 這可能會打破很多你的數據庫結構,並需要重大升級!你真的需要Django 1.5嗎?