2017-02-05 1 views
1

Я изменил поле manytomany в поле токена с помощью загрузочного модуля javascript. Затем я получаю запятую (,) отделяет первичные ключи как входные данные. После разделения из разделителя запятой (,) я получил экземпляр точного объекта. Теперь, как добавить эти объекты в поле manytomany. Я попробовал вернуть список, но это не сработало.Что такое возвращаемый тип clean_formField поля manytomany в django? Могу ли я вернуть список из функции

Вот мой класс формы

class EventRegistrationForm(forms.ModelForm): 
    participants = forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off', 'data-role':'tagsinput'}),required=True) 

    class Meta: 
     model=EventRegistration 
     fields=['event', 'participants', 'teamName', 'feePaid'] 

    def clean_feePaid(self): 
     feepaid=self.cleaned_data.get('feePaid') 
     if not feepaid: 
      raise ValidationError('Please pay the fee First :)') 
     return feepaid 

    def clean_participants(self): 
     participants_data = self.cleaned_data.get('participants') 
     event = self.cleaned_data.get('event') 
     participants =[] 

     for pd in participants_data.split(','): 
      p = Candidate.objects.get(pk=pd) 
      participants.append(p) 
     if not (event.minParticipant <= len(participants) <= event.maxParticipant): 
      raise ValidationError('Number of Participants exceeded :D') 
     return participants 

    def __init__(self, *args, **kwargs): 
     super(EventRegistrationForm, self).__init__(*args, **kwargs) 
     self.fields['event'].empty_label = '' 
     # following line needed to refresh widget copy of choice list 
     self.fields['event'].widget.choices =self.fields['event'].choices 

ответ

0

можно передать список для поля М2М. Я просто пропустил form.save_m2m().

Смежные вопросы