2024-10-05 17:30:35 +02:00
|
|
|
from rest_framework import serializers
|
2025-03-02 10:43:19 +01:00
|
|
|
from .models import Plant, PlantImage
|
|
|
|
|
|
|
|
|
|
class PlantImageSerializer(serializers.ModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = PlantImage
|
|
|
|
|
fields = ['id', 'image'] # Fields to expose for each image
|
|
|
|
|
|
2024-10-05 17:30:35 +02:00
|
|
|
|
|
|
|
|
class PlantSerializer(serializers.ModelSerializer):
|
2025-03-02 10:43:19 +01:00
|
|
|
images = PlantImageSerializer(many=True, read_only=True) # Include related images
|
|
|
|
|
|
2024-10-05 17:30:35 +02:00
|
|
|
class Meta:
|
|
|
|
|
model = Plant
|
2025-03-02 10:43:19 +01:00
|
|
|
fields = ['id', 'name', 'description', 'buy_price', 'sell_price', 'stock', 'images'] # Add the `images` field
|
2024-10-05 17:30:35 +02:00
|
|
|
extra_kwargs = {
|
2025-03-02 10:43:19 +01:00
|
|
|
'buy_price': {'required': False, 'allow_null': True},
|
|
|
|
|
'sell_price': {'required': False, 'allow_null': True},
|
|
|
|
|
'description': {'required': False, 'allow_null': True},
|
2024-10-05 17:30:35 +02:00
|
|
|
}
|