简体   繁体   中英

Django - get_or_create() with auto_now=True

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.

my model:

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)

the script:

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'])

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.

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

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

That probably means you are using the wrong function. You should be using update_or_create istead.

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.

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.

However, changing to this method doesn't solve this:

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

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

class Movie(models.Model):
    title = models.CharField(max_length=511)
    tmdb_id = models.IntegerField(unique=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM