From ec6d3dd172cd2b88cded33638d9c257ee029e3be Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Thu, 30 Oct 2025 16:21:26 +0500 Subject: [PATCH] wherehouse: fix bug --- ...018_alter_invalidproduct_project_folder.py | 20 +++++++++++++++++++ .../apps/wherehouse/models/invalid_product.py | 3 ++- .../wherehouse/serializers/invalid_product.py | 13 ++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 core/apps/wherehouse/migrations/0018_alter_invalidproduct_project_folder.py diff --git a/core/apps/wherehouse/migrations/0018_alter_invalidproduct_project_folder.py b/core/apps/wherehouse/migrations/0018_alter_invalidproduct_project_folder.py new file mode 100644 index 0000000..860cfe9 --- /dev/null +++ b/core/apps/wherehouse/migrations/0018_alter_invalidproduct_project_folder.py @@ -0,0 +1,20 @@ +# Generated by Django 5.2.4 on 2025-10-30 16:19 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('projects', '0016_estimatework_employee_estimatework_end_date_and_more'), + ('wherehouse', '0017_wherehouse_users'), + ] + + operations = [ + migrations.AlterField( + model_name='invalidproduct', + name='project_folder', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='invalid_products', to='projects.projectfolder'), + ), + ] diff --git a/core/apps/wherehouse/models/invalid_product.py b/core/apps/wherehouse/models/invalid_product.py index 1a030d3..5e8903e 100644 --- a/core/apps/wherehouse/models/invalid_product.py +++ b/core/apps/wherehouse/models/invalid_product.py @@ -26,7 +26,8 @@ class InvalidProduct(BaseModel): # relationship inventory = models.ForeignKey(Inventory, on_delete=models.CASCADE, related_name='invalid_products') project_folder = models.ForeignKey( - ProjectFolder, on_delete=models.CASCADE, related_name='invalid_products' + ProjectFolder, on_delete=models.CASCADE, related_name='invalid_products', + null=True, blank=True ) witnesses = models.ManyToManyField(User, related_name='invalid_products') work = models.ForeignKey( diff --git a/core/apps/wherehouse/serializers/invalid_product.py b/core/apps/wherehouse/serializers/invalid_product.py index 6477f92..020e2da 100644 --- a/core/apps/wherehouse/serializers/invalid_product.py +++ b/core/apps/wherehouse/serializers/invalid_product.py @@ -10,7 +10,7 @@ from core.apps.accounts.serializers.user import UserListSerializer class InvalidProductCreateSerializer(serializers.Serializer): inventory_id = serializers.UUIDField() - project_folder_id = serializers.UUIDField() + project_folder_id = serializers.UUIDField(required=False) witnesses_ids = serializers.ListField(child=serializers.UUIDField()) work_id = serializers.UUIDField(required=False) amount = serializers.IntegerField() @@ -24,16 +24,17 @@ class InvalidProductCreateSerializer(serializers.Serializer): inventory = Inventory.objects.filter(id=attrs['inventory_id']).first() if not inventory: raise serializers.ValidationError("Inventory not found") - project_folder = ProjectFolder.objects.filter(id=attrs['project_folder_id']).first() - if not project_folder: - raise serializers.ValidationError("Project Folder not found") + if attrs.get('project_folder_id'): + project_folder = ProjectFolder.objects.filter(id=attrs['project_folder_id']).first() + if not project_folder: + raise serializers.ValidationError("Project Folder not found") + attrs['project_folder'] = project_folder if attrs.get('work_id'): work = EstimateWork.objects.filter(id=attrs['work_id']).first() if not work: raise serializers.ValidationError("Work not found") attrs['work'] = work attrs['inventory'] = inventory - attrs['project_folder'] = project_folder return super().validate(attrs) def create(self, validated_data): @@ -75,7 +76,7 @@ class InvliadProductListSerializer(serializers.ModelSerializer): return { 'id': obj.project_folder.id, 'name': obj.project_folder.name, - } + } if obj.project_folder else None def get_wherehouse(self, obj): return {