Django - TypeError:'genre'是此函数的无效关键字参数

时间:2016-09-16 21:56:55

标签: python django

我正在使用Django,我在运行使用Django模型的Python脚本时遇到问题

我正在使用的脚本从api获取数据并将其加载到我的数据库中,但是,我在尝试运行时遇到TypeError:

>>> exec(open('load_from_api.py').read())
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\commands\shell.py", line 69, in handle
    self.run_shell(shell=options['interface'])
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\commands\shell.py", line 61, in run_shell
    raise ImportError
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\models\qu
ery.py", line 465, in get_or_create
    return self.get(**lookup), False
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\models\qu
ery.py", line 387, in get
    self.model._meta.object_name
movies.models.DoesNotExist: Movie matching query does not exist.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<string>", line 24, in <module>
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\models\ma
nager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\models\qu
ery.py", line 467, in get_or_create
    return self._create_object_from_params(lookup, params)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\models\qu
ery.py", line 499, in _create_object_from_params
    obj = self.create(**params)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\models\qu
ery.py", line 399, in create
    obj = self.model(**kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\db\models\ba
se.py", line 443, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % li
st(kwargs)[0])
TypeError: 'genre' is an invalid keyword argument for this function

电影模式:

class Movie(models.Model):
    title = models.CharField(max_length=511)
    tmdb_id = models.IntegerField(null=True, blank=True)
    release = models.DateField(null=True, blank=True)
    poster = models.TextField(max_length=500)
    backdrop = models.TextField(max_length=500, null=True, blank=True)
    rating = models.FloatField(null=True, blank=True)
    description = models.TextField()
    director = models.ManyToManyField(Person, related_name="directed_movies")
    actors = models.ManyToManyField(Person, related_name="acted_movies")
    genre = models.ManyToManyField(Genre)

以及我正在使用的脚本的片段:

for movie in results:
    data = Movie.objects.get_or_create(title=movie['title'],
                                       tmdb_id=movie['id'],
                                       release=movie['release_date'],
                                       genre=movie['genre_ids'],
                                       description=movie['overview'],
                                       backdrop=movie['backdrop_path'],
                                       poster=movie['poster_path'])

movie ['genre_ids']以列表形式出现:[42,67,80]或[12,109]。

我不确定我哪里错了,任何帮助都会受到赞赏!

修改

当我将脚本更改为:

for movie in results:
    data = Movie.objects.get_or_create(title=movie['title'],
                                       tmdb_id=movie['id'],
                                       release=movie['release_date'],
                                       description=movie['overview'],
                                       backdrop=movie['backdrop_path'],
                                       poster=movie['poster_path'])
    genres = []
    genres = movie['genre_ids']
    for gnre in genres:
        data.genre.add(gnre)

它给了我以下错误:

AttributeError: 'tuple' object has no attribute 'genre'

2 个答案:

答案 0 :(得分:0)

试试这个:

for movie in results:
    obj, data = Movie.objects.get_or_create(title=movie['title'],
                                       tmdb_id=movie['id'],
                                       release=movie['release_date'],
                                       description=movie['overview'],
                                       backdrop=movie['backdrop_path'],
                                       poster=movie['poster_path'])
    genre_ids = movie['genre_ids']
    genres = Genre.objects.filter(id__in=genre_ids)
    obj.genre.add(*genres)

答案 1 :(得分:0)

  

get_or_create()

     

返回(object,created)的元组,其中object是检索到的或   创建的对象和创建的是一个布尔值,指定是否为新的   对象已创建。

所以代替:

data = Movie.objects.get_or_create(title=movie['title'],
                                   ....)

它应该是:

data, created = Movie.objects.get_or_create(title=movie['title'],
                                            ....)