python - Django中类似事务的实现方法?
怪我咯
怪我咯 2017-04-18 10:33:16
0
1
436
class Category(models.Model):
    c_name = models.CharField(max_length=100)
    
    
class Item(models.Model):
    """外键关联到Category"""
    i_name = models.CharField(max_length=100)
    category = models.ForeignKey('Category', related_name='items',
                                 null=True, blank=True)

表结构是这样, 要实现在页面上新建一个分类的时候, 可以在同一个页面新建/编辑这个分类包含的项目和项目具体信息

目前想到的做法是, 页面上有添加项目的按钮, 点击按钮的时候就通过Ajax在数据库中生成了新项目, 然后在保存分类的时候, 再把刚才的项目和分类做关联;
但这样做的问题是, 如果编辑/新建分类的时候刷新了页面, 或者其他原因没有最终点击保存的话, 数据库中会增加很多没有外键的Item数据

请问实现这个功能更好的解决办法是什么? 或者说有什么办法, 判断在分类没有成功保存的情况下, 回滚之前建立的项目?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
迷茫

I think the page design logic can be changed. First, improve the classification information, save the classification information, and then add items to this classification information. In this way, when submitting project information, the classification information must be available.
As for the situation you mentioned, if you create a project first and then assign a category, there will definitely be uncategorized projects. How about adding a page to view all uncategorized projects and allowing users to set categories?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!