fixed is_stock_low
This commit is contained in:
parent
530fd6de08
commit
c0cbfd63d9
@ -11,7 +11,6 @@ class Product(models.Model):
|
|||||||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="products")
|
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="products")
|
||||||
stock_limit = models.IntegerField(null=True, blank=True)
|
stock_limit = models.IntegerField(null=True, blank=True)
|
||||||
alert_enabled = models.BooleanField(default=False)
|
alert_enabled = models.BooleanField(default=False)
|
||||||
alert_message = models.CharField(max_length=200, null=True, blank=True,help_text="Message d'alerte")
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_stock_low(self):
|
def is_stock_low(self):
|
||||||
|
@ -33,8 +33,20 @@ class UserSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
class ProductSerializer(serializers.ModelSerializer):
|
class ProductSerializer(serializers.ModelSerializer):
|
||||||
user = serializers.PrimaryKeyRelatedField(read_only=True)
|
user = serializers.PrimaryKeyRelatedField(read_only=True)
|
||||||
is_stock_low = serializers.BooleanField(read_only=True)
|
is_stock_low = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
def validate(self, data):
|
||||||
|
if data.get('alert_enabled') and data.get('stock_limit') is None:
|
||||||
|
raise serializers.ValidationError({
|
||||||
|
'product_stock_limit': "Ce champ est obligatoire si une alerte est activée."
|
||||||
|
})
|
||||||
|
return data
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
fields = ["id", "name", "description", "quantity", "creation_date", "modification_date", "user","stock_limit","alert_enabled","alert_message","is_stock_low"]
|
fields = ["id", "name", "description", "quantity", "creation_date", "modification_date", "user","stock_limit","alert_enabled","is_stock_low"]
|
||||||
|
|
||||||
|
def get_is_stock_low(self, obj):
|
||||||
|
if not isinstance(obj, Product):
|
||||||
|
return None
|
||||||
|
return obj.is_stock_low
|
Loading…
x
Reference in New Issue
Block a user