unique slugs even in the same news object
This commit is contained in:
parent
23f8c0bbcd
commit
e8538d4fc2
|
|
@ -232,12 +232,13 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
|
|||
|
||||
def create(self, validated_data):
|
||||
slugs = validated_data.get('slugs')
|
||||
|
||||
slugs_list = list(map(lambda x: x.lower(), slugs.values()))
|
||||
slugs_set = set(slugs_list)
|
||||
if slugs:
|
||||
if models.News.objects.filter(
|
||||
slugs__values__contains=list(slugs.values())
|
||||
).exists():
|
||||
raise serializers.ValidationError({'slugs': _('News with this slug already exists.')})
|
||||
).exists() or len(slugs_list) != len(slugs_set):
|
||||
raise serializers.ValidationError({'slugs': _('Slug should be unique')})
|
||||
|
||||
request = self.context.get("request")
|
||||
if request and hasattr(request, "user"):
|
||||
|
|
@ -248,11 +249,13 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
|
|||
|
||||
def update(self, instance, validated_data):
|
||||
slugs = validated_data.get('slugs')
|
||||
slugs_list = list(map(lambda x: x.lower(), slugs.values()))
|
||||
slugs_set = set(slugs_list)
|
||||
if slugs:
|
||||
if models.News.objects.filter(
|
||||
slugs__values__contains=list(slugs.values())
|
||||
).exclude(pk=instance.pk).exists():
|
||||
raise serializers.ValidationError({'slugs': _('News with this slug already exists.')})
|
||||
).exists() or len(slugs_list) != len(slugs_set):
|
||||
raise serializers.ValidationError({'slugs': _('Slug should be unique')})
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user