class Meta:

        model = SimpleAd
        fields = ('title', 'body', 'price', 'author', 'slug',
                  'date_of_publication')
        validators = [
            UniqueForYearValidator(queryset=SimpleAd.objects.all(),
                                   field='slug',
                                   date_field='date_of_publication'),
        ]
Exemplo n.º 2
0
    def get_validators(self):
        # If the validators have been declared explicitly then use that.
        validators = getattr(getattr(self, 'Meta', None), 'validators', None)
        if validators is not None:
            return validators

        # Determine the default set of validators.
        validators = []
        model_class = self.Meta.model
        field_names = set([
            field.source for field in self.fields.values()
            if (field.source != '*') and ('.' not in field.source)
        ])

        # Note that we make sure to check `unique_together` both on the
        # base model class, but also on any parent classes.
        for parent_class in [model_class] + list(model_class._meta.parents.keys()):
            for unique_together in parent_class._meta.unique_together:
                if field_names.issuperset(set(unique_together)):
                    validator = UniqueTogetherValidator(
                        queryset=parent_class._default_manager,
                        fields=unique_together
                    )
                    validators.append(validator)

        # Add any unique_for_date/unique_for_month/unique_for_year constraints.
        info = model_meta.get_field_info(model_class)
        for field_name, field in info.fields_and_pk.items():
            if field.unique_for_date and field_name in field_names:
                validator = UniqueForDateValidator(
                    queryset=model_class._default_manager,
                    field=field_name,
                    date_field=field.unique_for_date
                )
                validators.append(validator)

            if field.unique_for_month and field_name in field_names:
                validator = UniqueForMonthValidator(
                    queryset=model_class._default_manager,
                    field=field_name,
                    date_field=field.unique_for_month
                )
                validators.append(validator)

            if field.unique_for_year and field_name in field_names:
                validator = UniqueForYearValidator(
                    queryset=model_class._default_manager,
                    field=field_name,
                    date_field=field.unique_for_year
                )
                validators.append(validator)

        return validators
 class Meta:
     """Meta class to map serializer's fields with the model fields."""
     model = Movie
     fields = "__all__"
     Validators = [
         UniqueForYearValidator(
             queryset=Movie.objects.all(),
             field='title',
             date_field='release_date',
             message='You can release only one movie per year.'
         )
     ]
 class Meta:
     model = News
     fields = (
         'title',
         'link',
         'which_site',
         'date_of_publication',
     )
     validators = [
         UniqueForYearValidator(queryset=News.objects.all(),
                                field='title',
                                date_field='date_of_publication'),
     ]