adding crud to the front end

This commit is contained in:
2025-08-09 17:41:41 +03:30
parent f3ea216219
commit 82d9095959
7 changed files with 96 additions and 16 deletions

View File

@@ -69,16 +69,16 @@ class Job(models.Model):
("Pruning", "PRUNING"),
("Harvesting", "HARVESTING"),
]
type = models.CharField(max_length=15, choices=TYPE_CHOICES, default="Irrigating")
type = models.CharField(max_length=15, choices=TYPE_CHOICES, default="Irrigation")
field = models.ForeignKey(Field, on_delete=models.CASCADE)
made_date = models.DateTimeField(auto_now_add=True)
date = models.DateField()
status = models.BooleanField()
costs = models.ManyToManyField(Cost, null=True, blank=True)
notes = models.ManyToManyField(Note, null=True, blank=True)
status = models.BooleanField(default=False)
costs = models.ManyToManyField(Cost, blank=True)
notes = models.ManyToManyField(Note, blank=True)
##########REMOVE THIS MF!
# voices = models.ForeignKey(Voice, on_delete=models.SET_NULL, null=True, blank=True)
images = models.ManyToManyField(Image, null=True, blank=True)
images = models.ManyToManyField(Image, blank=True)
def __str__(self):
return self.type

View File

@@ -35,25 +35,105 @@ class ImageSerializer(serializers.ModelSerializer):
class JobSerializer(serializers.ModelSerializer):
# field = serializers.PrimaryKeyRelatedField(queryset=Field.objects.all())
field = FieldSerializer(read_only=True)
# costs = serializers.PrimaryKeyRelatedField(
# queryset=Cost.objects.all(), required=False, allow_null=True
# )
field = serializers.PrimaryKeyRelatedField(queryset=Field.objects.all())
costs = CostSerializer(read_only=True, many=True)
# notes = serializers.PrimaryKeyRelatedField(
# queryset=Note.objects.all(), required=False, many=True
# )
cost_ids = serializers.PrimaryKeyRelatedField(
queryset=Cost.objects.all(), many=True, write_only=True, required=False
)
notes = NoteSerializer(read_only=True, many=True)
# images = serializers.PrimaryKeyRelatedField(
# queryset=Image.objects.all(), required=False, many=True
# )
note_ids = serializers.PrimaryKeyRelatedField(
queryset=Note.objects.all(), many=True, write_only=True, required=False
)
images = ImageSerializer(read_only=True, many=True)
image_ids = serializers.PrimaryKeyRelatedField(
queryset=Image.objects.all(), many=True, write_only=True, required=False
)
class Meta:
model = Job
fields = "__all__"
def create(self, validated_data):
print("validated_data:", validated_data)
note_ids = validated_data.pop("note_ids", [])
cost_ids = validated_data.pop("cost_ids", [])
image_ids = validated_data.pop("image_ids", [])
note_ids = [n.id if hasattr(n, "id") else n for n in note_ids]
cost_ids = [c.id if hasattr(c, "id") else c for c in cost_ids]
image_ids = [i.id if hasattr(i, "id") else i for i in image_ids]
job = Job.objects.create(**validated_data)
if note_ids:
notes = Note.objects.filter(id__in=note_ids)
job.notes.add(*notes)
if cost_ids:
costs = Cost.objects.filter(id__in=cost_ids)
job.costs.add(*costs)
if image_ids:
images = Image.objects.filter(id__in=image_ids)
job.images.add(*images)
return job
def update(self, instance, validated_data):
note_ids = validated_data.pop("note_ids", None)
cost_ids = validated_data.pop("cost_ids", None)
image_ids = validated_data.pop("image_ids", None)
if note_ids is not None:
note_ids = [n.id if hasattr(n, "id") else n for n in note_ids]
if cost_ids is not None:
cost_ids = [c.id if hasattr(c, "id") else c for c in cost_ids]
if image_ids is not None:
image_ids = [i.id if hasattr(i, "id") else i for i in image_ids]
for attr, value in validated_data.items():
setattr(instance, attr, value)
instance.save()
if note_ids is not None:
notes = Note.objects.filter(id__in=note_ids)
instance.notes.set(notes)
if cost_ids is not None:
costs = Cost.objects.filter(id__in=cost_ids)
instance.costs.set(costs)
if image_ids is not None:
images = Image.objects.filter(id__in=image_ids)
instance.images.set(images)
return instance
# class JobSerializer(serializers.ModelSerializer):
# # field = serializers.PrimaryKeyRelatedField(queryset=Field.objects.all())
# field = FieldSerializer(read_only=True)
# # costs = serializers.PrimaryKeyRelatedField(
# # queryset=Cost.objects.all(), required=False, allow_null=True
# # )
# costs = CostSerializer(read_only=True, many=True)
# # notes = serializers.PrimaryKeyRelatedField(
# # queryset=Note.objects.all(), required=False, many=True
# # )
# notes = NoteSerializer(read_only=True, many=True)
# # images = serializers.PrimaryKeyRelatedField(
# # queryset=Image.objects.all(), required=False, many=True
# # )
# images = ImageSerializer(read_only=True, many=True)
# class Meta:
# model = Job
# fields = "__all__"
class CultivationCalenderSerializer(serializers.ModelSerializer):
class Meta:

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 840 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB