Django - get_or_create() with auto_now=True
<p>Iâm using Django and I'm having a problem with a Python script that uses Django models
The script that I'm using takes data from an api and loads it into my database.</p>

<p>my model:</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, null=True)
    runtime = models.IntegerField(null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    edit = models.DateTimeField(auto_now=True, null=True, blank=True)
    backdrop = models.TextField(max_length=500, null=True, blank=True)
    popularity = models.TextField(null=True, blank=True)
</code></pre>

<p>the script:</p>

<pre><code>movies = tmdb.Movies().upcoming()
results = movies['results']
ids = []
for movie in results:
    data, created = 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'],
                                                popularity=movie['popularity'])
</code></pre>

<p>The problem I'm having is that whenever I run the script, the entries are duplicated because the edit field is change, but the purpose I put the edit field is to know when exactly a movie got edited, ie: some other field got changed.</p>

<p>How can I avoid the duplicates, but also keep the edit field in case some real change happened?</p>

<blockquote>
  <p>but the purpose I put the edit field is to know when exactly a movie
  got edited, ie: some other field got changed.</p>
</blockquote>

<p>That probably means you are using the wrong function. You should be using <a href="https://docs.djangoproject.com/en/1.10/ref/models/querysets/#update-or-create" rel="nofollow">update_or_create</a> istead.</p>

<blockquote>
  <p>A convenience method for updating an object with the given kwargs,
  creating a new one if necessary. The defaults is a dictionary of
  (field, value) pairs used to update the object.</p>
</blockquote>

<p>This is different from get_or_create, which creates an object if it does not exists, or simply fetches it when it does exist. update_or_create is the one that does the actually updating. </p>

<p>However, changing to this method doesn't solve this:</p>

<blockquote>
  <p>How can I avoid the duplicates, but also keep the edit field in case
  some real change happened?</p>
</blockquote>

<p>Duplicates are created because you do not have a unique index on any of your fields. Both <code>get_or_create</code> and <code>update_or_create</code> require that you have a unique field. It seems that the following change is in order:</p>

<pre><code>class Movie(models.Model):
    title = models.CharField(max_length=511)
    tmdb_id = models.IntegerField(unique=True)
</code></pre>

python django