Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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
在Django中使用for循环保存多个列值_Django_Python 3.x_Django Models_Django Views - Fatal编程技术网

在Django中使用for循环保存多个列值

在Django中使用for循环保存多个列值,django,python-3.x,django-models,django-views,Django,Python 3.x,Django Models,Django Views,在我的数据库(SQLite)中,我希望通过使用字典中的for循环来更新给定行中的每一列。我已经搜索了SO和Django文档,但是找不到任何关于通过for循环更新多个列的信息。我怀疑有一个简单的方法可以解决我的问题,但我已经没有谷歌的关键词了 下面是一个非常简单的例子,可以将问题归结为其核心: models.py from django.db import models class author_ratings(models.Model): name = models.TextField

在我的数据库(SQLite)中,我希望通过使用字典中的for循环来更新给定行中的每一列。我已经搜索了SO和Django文档,但是找不到任何关于通过for循环更新多个列的信息。我怀疑有一个简单的方法可以解决我的问题,但我已经没有谷歌的关键词了

下面是一个非常简单的例子,可以将问题归结为其核心:

models.py

from django.db import models

class author_ratings(models.Model):
    name = models.TextField()
    book_one = models.IntegerField()
    book_two = models.IntegerField()
    book_three = models.IntegerField()
views.py

from models import author_ratings

ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}

current_author = author_ratings.objects.filter(name="Jim")

for book,rating in ratings_dictionary:
    current_author.book = rating
    current_author.save()
问题:Django没有将10、20和30的值保存到作者行的相应列中,而是抛出以下错误:

“AttributeError:'当前作者'对象没有'book'属性”

编辑: 在下面的回答中,Shafikur Rahman正确地指出“book”不是可用字段(只有“name”、“book\u one”、“book\u two”和“book\u three”可用)。我的问题是:如何使用for循环更新每个字段而不必硬编码所有字段

例如,我希望避免显式键入每一列:

current_author.book_one = ratings_dictionary['book_one']
current_author.book_two = ratings_dictionary['book_two']
current_author.book_three = ratings_dictionary['book_three']
current_author.save()
以上信息应足以充分理解问题,但我可以根据要求提供任何其他详细信息。
提前感谢您的帮助。

您的
作者评分
模型没有字段
书籍
,因此它是错误的。可用的字段有
name
book\u one
book\u two
,和
book\u two
,您的
作者评分
模型没有字段
book
,因此它是错误的。在views.py中可用的字段有
name
book\u one
book\u two
book\u three
。您可以:

from models import author_ratings

ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}

current_author = author_ratings.objects.filter(name="Jim")

current_author.update(**ratings_dictionary)
您可以在views.py中阅读有关更新的信息。您可以:

from models import author_ratings

ratings_dictionary = {'book_one': 10, 'book_two': 20, 'book_three': 30}

current_author = author_ratings.objects.filter(name="Jim")

current_author.update(**ratings_dictionary)

您可以使用
current\u author=author\u ratings.objects.filter(name=“Jim”)
阅读
update

,您将返回一个
QuerySet
,而不是模型的实例。这样做

   current_author.book_one = ratings_dictionary['book_one']
   current_author.book_two = ratings_dictionary['book_two']
   current_author.book_three = ratings_dictionary['book_three']
   current_author.save()
你仍然会出错<代码>“QuerySet”对象至少没有属性“save”

但是QuerySet有
update
方法,如果您不坚持使用循环,它可以为您节省几行代码:


current\u author.update(**ratings\u dictionary)
就是使用
current\u author=author\u ratings.objects.filter(name=“Jim”)
返回的是
查询集,而不是模型实例。这样做

   current_author.book_one = ratings_dictionary['book_one']
   current_author.book_two = ratings_dictionary['book_two']
   current_author.book_three = ratings_dictionary['book_three']
   current_author.save()
你仍然会出错<代码>“QuerySet”对象至少没有属性“save”

但是QuerySet有
update
方法,如果您不坚持使用循环,它可以为您节省几行代码:


current\u author.update(**ratings\u dictionary)
是您所需要的一切

感谢您的回复。我想我的问题是:如何使用for循环来避免硬编码每个字段?例如,如果我有100列需要更新,我不想列出每个作者的所有100个字段。Ok。有些事我需要澄清。你能介绍一下你的“评级词典”吗?谢谢你的回复。我想我的问题是:如何使用for循环来避免硬编码每个字段?例如,如果我有100列需要更新,我不想列出每个作者的所有100个字段。Ok。有些事我需要澄清。你能介绍一下你的“评级词典”吗?