2012-01-02 132 views
0

我想添加多個VIDEO_ID(videolinks)同時wtih相同的標題和類別這裏是我的表單代碼,請做一些幫助的Django的ModelForm

class AddVideoViaLinkForm(forms.ModelForm): 
    title = forms.CharField(required=True,min_length=1,max_length=200) 
    video_id = forms.CharField(required=True,min_length=1,max_length=20) 
    category = forms.CharField(required=True,min_length=1,max_length=20) 
    description = forms.CharField(required=True,min_length=1,max_length=20) 

    def clean_title(self): 
     title = self.cleaned_data.get('title') 
     if title.strip()=='': 
      raise forms.ValidationError('Enter a Valid Title') 
     else: 
      return title  
    def clean_description(self): 
     description = self.cleaned_data.get('description') 
     if description.strip()=='': 
      raise forms.ValidationError('Enter a Valid Description') 
     else: 
      return description  

class Meta: 
    model = Videos 
     fields = ('title','video_id','category','description') 

回答

0

所有元首先應該屬於AddVideoViaLinkForm。在這個類中,你可以添加保存功能,這將爲你做這個:

def save(self): 
    ids = self.cleaned_data['video_id'].split(",")# it depends what is your id separator 
    title = self.cleaned_data['title'] 
    for id in ids: 
     Videos.objects.create(video_id=id, title=title, ...)