簡體   English   中英

Django模型中的if語句如何設置字段?

[英]How to set a field will be change by if statement in models for Django?

我在模型中有一個字段,True/False 值會受到其他字段的影響。 我可以知道如何設置嗎?

模型.py:

class Product(models.Model):
  storage_amount = models.PositiveIntegerField()
  if storage_amount == 0 or None:
    out_of_storage_or_not = models.BooleanField(default=True)
  else:
    out_of_storage_or_not = models.BooleanField(default=False)
  class Product(models.Model):
    storage_amount = models.PositiveIntegerField()
    out_of_storage_or_not = models.BooleanField(default=False)

    def save(self,*args, **kwargs):
      if self.storage_amount==0 or None:
        self.out_of_storage_or_not=True
      super().save(*args, **kwargs)

嘗試這個

class Product(models.Model):
    storage_amount = models.PositiveIntegerField()
    out_of_storage_or_not = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        self.out_of_storage_or_not = False if self.storage_amount else True
        super().save(*args, **kwargs)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM