zoukankan      html  css  js  c++  java
  • django model高阶用法(字段加密、singal、customManager等)

    Django - Update model field based on another field

    class Item(models.Model):
        name = models.CharField(max_length=40)
        default_price = models.DecimalField(max_digits=6, decimal_places=2, default=50)
    
        def __unicode__(self):
            return self.name
    
    class SaleDetail(models.Model):
        item = models.ForeignKey(Item)
        deposit = models.ForeignKey(Deposit)
        quantity = models.PositiveIntegerField()
        entered_unit_price = models.DecimalField(max_digits=6, decimal_places=2, default=None)
        sale = models.ForeignKey(Sale)
    
        @property
        def unit_price(self, value):
            if self.entered_unit_price is None:
                return self.item.default_price
            else:
                return self.entered_unit_price
    
        @unit_price.setter
        def unit_price(self, value):
            self.entered_unit_price = value

    Then use it like so:

    print(sd.unit_price)
    sd.unit_price = 500 
    sd.save()

    更好点的用法

     check if there is a pk. If the pk is None, we know that the SaleDetail is new. Then, we see if there is a unit_price. If there is not a unit_price, we set it to the Item.default_price.

    class Item(models.Model):
        name = models.CharField(max_length=40)
        default_price = models.DecimalField(max_digits=6, decimal_places=2, default=50)
    
        def __unicode__(self):
            return self.name
    
    
    class SaleDetail(models.Model):
        item = models.ForeignKey(Item)
        deposit = models.ForeignKey(Deposit)
        quantity = models.PositiveIntegerField()
        unit_price = models.DecimalField(max_digits=6, decimal_places=2)
        sale = models.ForeignKey(Sale)
    
        def save(self, *args, **kwargs):
            # Make sure this is the first save (pk should be None) and there is no unit_price set
            if self.pk is None and not self.unit_price:
                self.unit_price = self.item.default_price
            elif not self.unit_price:
                self.unit_price = self.item.default_price
    
            # Call the original save method
            super(SaleDetail, self).save(*args, **kwargs)

    关于signal:

    This calls  post_save 信号会被调用

    user = User.objects.get(id=1) 
    user.username='edited_username' 
    user.save()
    

    This does not call post_save 这种写法,写好不会被调用

    User.objects.filter(id=1).update(username='edited_username')

    学习signal:https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html


    深度定制django model:定制managerhttps://docs.djangoproject.com/en/3.1/topics/db/managers/

    hidden隐藏字段

    Change a Django form field to a hidden field

     
  • 相关阅读:
    firefox 网页编辑器不能访问剪贴板图片的问题解决
    ubuntu 安装rails 遇到的问题及解决办法
    Mysql索引建立、查询、删除
    UIImage 各种处理(分类)
    iOS加载Gif图片
    json plist 互转
    克隆网站 mac
    UIlabel
    pod 更新 升级
    2016审核标准
  • 原文地址:https://www.cnblogs.com/lxgbky/p/13825345.html
Copyright © 2011-2022 走看看