Fix favorites creation by slug
This commit is contained in:
parent
ecdf2fcaa0
commit
f38924db42
|
|
@ -315,20 +315,22 @@ class EstablishmentFavoritesCreateSerializer(serializers.ModelSerializer):
|
|||
def validate(self, attrs):
|
||||
"""Override validate method"""
|
||||
# Check establishment object
|
||||
establishment_id = self.context.get('request').parser_context.get('kwargs').get('pk')
|
||||
establishment_qs = models.Establishment.objects.filter(id=establishment_id)
|
||||
establishment_slug = self.context.get('request').parser_context.get('kwargs').get('slug')
|
||||
establishment_qs = models.Establishment.objects.filter(slug=establishment_slug)
|
||||
|
||||
# Check establishment obj by pk from lookup_kwarg
|
||||
# Check establishment obj by slug from lookup_kwarg
|
||||
if not establishment_qs.exists():
|
||||
raise serializers.ValidationError({'detail': _('Object not found.')})
|
||||
else:
|
||||
establishment = establishment_qs.first()
|
||||
|
||||
# Check existence in favorites
|
||||
if self.get_user().favorites.by_content_type(app_label='establishment',
|
||||
model='establishment')\
|
||||
.by_object_id(object_id=establishment_id).exists():
|
||||
.by_object_id(object_id=establishment.id).exists():
|
||||
raise utils_exceptions.FavoritesError()
|
||||
|
||||
attrs['establishment'] = establishment_qs.first()
|
||||
attrs['establishment'] = establishment
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data, *args, **kwargs):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user