示例#1
0
 class Meta:
     model = NullValidatingModel
     fields = '__all__'
     validators = [
         UniqueTogetherValidator(queryset=NullValidatingModel.objects,
                                 fields=('name', 'code'))
     ]
    def get_unique_together_validators(self):
        model = self.Meta.model
        validators = []
        field_names = set(self.get_field_names(self._declared_fields, self.field_info))

        uniq_indexes = filter(lambda i: i.get('unique', False), model._meta.get('index_specs', []))
        for idx in uniq_indexes:
            if not idx.get('unique', False):
                continue
            field_set = tuple(map(lambda e: e[0], idx['fields']))
            if len(field_set) > 1 and field_names.issuperset(set(field_set)):
                validators.append(UniqueTogetherValidator(
                    queryset=model.objects,
                    fields=field_set
                ))
        return validators
 class Meta:
     model = NonValidatingModel
     validators = [
         UniqueTogetherValidator(queryset=NonValidatingModel.objects,
                                 fields=('name', 'code'))
     ]