xzc19970719
V2EX  ›  Django

django 中 GenericForeignKey 关联的对象删除时 怎么样级联删除

  •  
  •   xzc19970719 · Oct 8, 2018 · 3191 views
    This topic created in 2783 days ago, the information mentioned may be changed or developed.

    比如我把一篇博客删了,我有一个点赞数 通过 GenericForeignKey('博客类', '博客 id') 关联到博客 请问怎么把这个点赞数也删了

    2 replies    2018-10-09 14:28:53 +08:00
    PythonAnswer
        1
    PythonAnswer  
       Oct 9, 2018
    models.ForeignKey('self', on_delete=models.CASCADE)
    WoodenRobot
        2
    WoodenRobot  
       Oct 9, 2018
    在被关联的博客 model 中增加一个 GenericRelation 关联点赞 model, 删除博客时就会级联删除相应的点赞数。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3061 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 04:09 · PVG 12:09 · LAX 21:09 · JFK 00:09
    ♥ Do have faith in what you're doing.