Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django中手动提交_Python_Django - Fatal编程技术网

Python 在django中手动提交

Python 在django中手动提交,python,django,Python,Django,在django模型方法中是否有更好的方法执行以下操作?以下内容看起来相当详细,因为我可以在SQL shell中用一行代码执行相同的操作: class ItemMaster(models.Model): ... @classmethod @transaction.commit_manually def update_imdb_rank(self): ''' Update all ranks from the IMDbEntry ta

在django模型方法中是否有更好的方法执行以下操作?以下内容看起来相当详细,因为我可以在SQL shell中用一行代码执行相同的操作:

class ItemMaster(models.Model):
    ...

    @classmethod
    @transaction.commit_manually
    def update_imdb_rank(self):
        '''
        Update all ranks from the IMDbEntry table
        '''
        cursor = connection.cursor()
        cursor.execute("UPDATE main_itemmaster i JOIN mturk_imdbentry m USING (imdb_url) SET i.imdb_rank=m.imdb_rank")
        transaction.commit()

如果愿意,可以使用
with
语句来减少冗长:

class ItemMaster(models.Model):
    ...

    @classmethod
    def update_imdb_rank(self):
        with transaction.atomic():
            cursor = connection.cursor()
            cursor.execute("UPDATE main_itemmaster i JOIN mturk_imdbentry m USING (imdb_url) SET i.imdb_rank=m.imdb_rank")

您是否有从main.ItemMaster到mturk.IMDBEntry或从mturk.IMDBEntry到main.ItemMaster的外键?它是基于
imdb\u url
?两者都没有外键,它们都是一样的。有没有理由不使用ORM进行更新?如果您希望使用ORM,那么除非您在两者之间添加FK或通用外键关系,否则您将无法进行更新。