Разрешите пользователю заполнять только одно из полей, а не оба | Django

Я создаю приложение наподобие reddit, где можно размещать видео и тексты.

Я хочу позволить пользователю выбирать между видео и изображением, но не между обоими

вот что я сделал:

class CreatePostForm(ModelForm):
    class Meta:
        model = Post
        fields = ['title','video','image','text']
        widgets ={'title':forms.TextInput({'placeholder':'تیتر'}), 'text':forms.Textarea({'placeholder':'متن'})}    
    
    def clean(self):
        cleaned_data = super().clean()
        text = cleaned_data.get("text")
        video = cleaned_data.get("video")
        if text is not None and video is not None:
            raise ValidationError('this action is not allowed', 'invalid')

это показывает validationError но даже когда пользователь заполняет только видео fileField а не texField все равно показывает validationError

Используйте required=False вместо blank = True

class CreatePostForm(ModelForm):
    video = forms.FileField(required=False)
    image = forms.FileField(required=False)
    ...
    def make_decision(text = None,video=None):
        if text != '' and video != '':
            print('--------- Both are not possible for selection ---------')
        elif text != '':
            text = '--------- calling only text ---------'
            print(text)
        elif video != '':
            video = '--------- calling only video ---------'
            print(video)
        else:
            print('--------- Please select atleast one ---------')


# ------ Possibility for calling function (test cases) --------------
# make_decision('y','y')   # raise_error = '--------- Both are not possible for selection ---------'
# make_decision('y','')  # '--------- calling only text ---------'
# make_decision('','y')  # '--------- calling only video ---------'
# make_decision('','') # '--------- Please select atleast one ---------'
Вернуться на верх