shared: district api unique_together field qoshildi
This commit is contained in:
@@ -36,6 +36,8 @@ class DistrictCreateSerializer(serializers.Serializer):
|
||||
if not user:
|
||||
raise serializers.ValidationError({"user_id": "Foydalanuvchi topilmadi"})
|
||||
data['user'] = user
|
||||
if District.objects.filter(name=data['name'], user=user).exists():
|
||||
raise serializers.ValidationError({'name': "District qo'shib bolmadi"})
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 5.2 on 2025-12-11 13:15
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('shared', '0017_alter_support_district'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterUniqueTogether(
|
||||
name='district',
|
||||
unique_together={('user', 'name')},
|
||||
),
|
||||
]
|
||||
@@ -14,3 +14,6 @@ class District(BaseModel):
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
unique_together = ('user', 'name')
|
||||
@@ -53,6 +53,9 @@ class DistrictCreateApiView(generics.CreateAPIView, ResponseMixin):
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
name = serializer.validated_data.get('name')
|
||||
if District.objects.filter(name=name, user=request.user).exists():
|
||||
return self.failure_response(message="District qo'shib bolmadi")
|
||||
|
||||
obj = District.objects.create(name=name, user=request.user)
|
||||
return self.success_response(
|
||||
data=district_serializers.DistrictSerializer(obj).data,
|
||||
|
||||
Reference in New Issue
Block a user