Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Sentence(models.Model):
- words = models.ManyToManyField(Word)
- language = models.ForeignKey(Language)
- def clean(self):
- for word in self.words.all():
- if word.language_id != self.language_id:
- raise ValidationError('One of the words has a false language')
- from django import forms
- class SentenceForm(forms.ModelForm):
- class Meta:
- model = Sentence
- 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
- class SentenceAdmin(admin.ModelAdmin):
- form = SentenceForm
- admin.register(Sentence, SentenceAdmin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement