V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
6167
V2EX  ›  Django

如何设置 django 模型,使得其中的 b 字段的初始值是 a 字段的值,并且 b 字段可修改

  •  
  •   6167 · 2018-05-24 16:54:04 +08:00 · 2709 次点击
    这是一个创建于 2135 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下 class Pool ( models.Model): amount=models.IntegerFields('数量') last=models.IntegerFields('剩余数量',default=0)

    目前用的是在模型下添加
    def save(self,*args,**kwargs):
    	self.last=self.amount
        super(Pool,self).save(*args,**kwargs)
    

    Pool 表是另外一个表的外键,再通过另一表的视图函数修改 last 字段时, 显示错误,要修改 last 必须修改 amount,求解。

    9 条回复    2018-05-25 18:27:52 +08:00
    6167
        1
    6167  
    OP
       2018-05-24 17:28:46 +08:00
    说的好像有点复杂,简单点说就是如何设置 last 字段的默认值 default,使得 last 字段的值等于 amount 字段的值
    twor
        2
    twor  
       2018-05-24 19:55:29 +08:00
    last=models.IntegerFields('剩余数量',default=-1)
    if self.last == -1 :
    ----self.last = self.amount

    写完,我自己也感觉不好意思
    fcfangcc
        3
    fcfangcc  
       2018-05-24 20:31:32 +08:00
    flask-sqlalchemy 里面可以这么用,可以搜索下 django 能不能这么用

    <script src="https://gist.github.com/fcfangcc/23e42a52f3faa6d3926f615074064197.js"></script>
    siteshen
        4
    siteshen  
       2018-05-25 01:10:37 +08:00
    看了下 Django 的代码,直接设置 default 做不到,因为 default 作为 callable 调用时,不会传入任何参数。
    你的答案方案在于,没有区分 self.last 的值,是“初始值”还是“被设置的值”。
    和 @twor 答案类似,不过用了一个无意义的变量作为 sentinel value。

    class Pool():
    UNSET = object()

    last = models.IntegerFields(default=UNSET)

    def save(self):
    if self.last == self.UNSET:
    self.last = self.amount

    super(Pool, self).save()
    6167
        5
    6167  
    OP
       2018-05-25 11:49:59 +08:00
    @siteshen 基本理解了,不过代码里的 UNSET 变量在执行 python manage.py migrate 命令的时候无法序列化,显示错误 ValueError:Cannot Serialize:object,有没有解决办法
    jinhao7773
        6
    jinhao7773  
       2018-05-25 13:04:09 +08:00
    为什么一定要设置值,而且重载 save 也不是万全的,比如直接改动数据库或使用 update()修改 amount,并不会同步修改 last,其实最好就是直接让 last 为 null,写个 get_last 方法返回 self.last or self.amount 就行,不直接去读 last。
    Gothack
        7
    Gothack  
       2018-05-25 13:18:40 +08:00
    用 @ receiver 就可以吧
    vZexc0m
        8
    vZexc0m  
       2018-05-25 13:29:56 +08:00
    利用 objectManager 可以实现。
    siteshen
        9
    siteshen  
       2018-05-25 18:27:52 +08:00
    @6167 放里面是为了最小权限原则,不行的话放外面。下面这个是 drf_yasg.utils 里的代码,用的 is 比较,比我之前写的完善些。

    class unset(object):
    """Used as a sentinel value for function parameters not set by the caller where ``None`` would be a valid value."""
    pass


    def swagger_auto_schema(method=None, methods=None, auto_schema=unset):
    if auto_schema is not unset:
    data['auto_schema'] = auto_schema
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1227 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 18:05 · PVG 02:05 · LAX 11:05 · JFK 14:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.