Django ManyToMany model validation

前端 未结 3 929
醉梦人生
醉梦人生 2020-12-02 20:30

I have a model with a ManyToManyField similar to this one (the model Word has a language, too):

class Sentence(models.Model):
    words = models.ManyToManyFi         


        
3条回答
  •  天涯浪人
    2020-12-02 20:56

    It is not possible to do this validation in the model's clean method, but you can create a model form which can validate the choice of words.

    from django import forms
    
    class SentenceForm(forms.ModelForm):
        class Meta:
            model = Sentence
            fields = ['words', 'language']
    
        def clean(self):
            """
            Checks that all the words belong to the sentence's language.
            """
            words = self.cleaned_data.get('words')
            language = self.cleaned_data.get('language')
            if language and words:
                # only check the words if the language is valid
                for word in words:
                    if words.language != language:
                        raise ValidationError("The word %s has a different language" % word)
            return self.cleaned_data
    

    You can then customise your Sentence model admin class, to use your form in the Django admin.

    class SentenceAdmin(admin.ModelAdmin):
        form = SentenceForm
    
    admin.register(Sentence, SentenceAdmin)
    

提交回复
热议问题