Django - TypeError: 'genre' is an invalid keyword argument for this function
<p>Iâm using Django and I'm having a problem running a Python script that uses Django models</p>

<p>The script that I'm using takes data from an api and loads it into my database, however, I'm getting a TypeError while trying to run it:</p>

<pre><code>&gt;&gt;&gt; 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 "&lt;console&gt;", line 1, in &lt;module&gt;
  File "&lt;string&gt;", line 24, in &lt;module&gt;
  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
</code></pre>

<p><strong>Movie model:</strong></p>

<pre><code>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)
</code></pre>

<p>and a snippet from the script i'm using:</p>

<pre><code>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'])
</code></pre>

<p>movie['genre_ids'] come in a list form: [42, 67, 80] or [12, 109] for example.</p>

<p>I'm not quiet sure where I'm wrong, any help would be appreciated!</p>

<p><strong>Edit:</strong></p>

<p>When i changed my script to this:</p>

<pre><code>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)
</code></pre>

<p>It gave me the following error:</p>

<pre><code>AttributeError: 'tuple' object has no attribute 'genre'
</code></pre>

<blockquote>
  <p>get_or_create()</p>
  
  <p>Returns a tuple of (object, created), where object is the retrieved or
  created object and created is a boolean specifying whether a new
  object was created.</p>
</blockquote>

<p>so instead of:</p>

<pre><code>data = Movie.objects.get_or_create(title=movie['title'],
                                   ....)
</code></pre>

<p>it should be:</p>

<pre><code>data, created = Movie.objects.get_or_create(title=movie['title'],
                                            ....)
</code></pre>

python django