- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Django,但在使用 Django 模型的 Python 脚本时遇到问题我正在使用的脚本从 api 获取数据并将其加载到我的数据库中。
我的模型:
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)
脚本:
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'])
我遇到的问题是,每当我运行脚本时,条目都会重复,因为编辑字段发生了变化,但我放置编辑字段的目的是了解电影何时被编辑,即:其他一些字段已更改。
如何避免重复,同时保留编辑字段,以防发生真正的更改?
最佳答案
but the purpose I put the edit field is to know when exactly a movie got edited, ie: some other field got changed.
这可能意味着您使用了错误的函数。您应该使用update_or_create相反。
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.
这与 get_or_create 不同,后者在对象不存在时创建该对象,或者在对象存在时简单地获取该对象。 update_or_create 是实际更新的那个。
但是,更改为此方法并不能解决此问题:
How can I avoid the duplicates, but also keep the edit field in case some real change happened?
创建重复项是因为您的任何字段都没有唯一索引。 get_or_create
和 update_or_create
都要求您有一个唯一的字段。似乎需要进行以下更改:
class Movie(models.Model):
title = models.CharField(max_length=511)
tmdb_id = models.IntegerField(unique=True)
关于python - Django - get_or_create() 与 auto_now=True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781666/
我有一个模型叫做零食: class Snack(models.Model): snack = models.CharField(max_length=9) 当我做的时候 Snack.objec
我有以下代码的语法错误: ids_row={} ids_row["releve_annee"]=int(row[0]) ids_row["releve_mois"]=int(row[1]) ids_r
尝试检查艺术家是否存在,如果不存在,则添加或链接到外键并保存。 这是模型 class Artist(models.Model): """Artist model""" title =
这是我的models.py: class Foo(models.Model): id = models.IntegerField(primary_key=True) name = mo
由于未知原因,我的 Django 模型中只有一个(18 个)抛出错误“类型对象‘LidarReading’没有属性‘get_or_create’”。模型声明如下。 class LidarReading
从官方文档看,我无法理解什么是默认参数。 obj, created = Person.objects.get_or_create(first_name='John', last_name='Lenno
我有一个只能使用 get_or_create(session=session) 访问的 Django 模型,其中 session 是另一个 Django 模型的外键。 因为我只通过 get_or_cr
Google App Engine 是否有 Django 的 get_or_create() 的等价物? ? 最佳答案 没有完全等价的,但是get_or_insert是类似的东西。主要区别在于 get
鉴于 object.get_or_create() 的全部意义在于获取对象(如果它已经存在),我不明白为什么它会抛出此代码的完整性错误: class UserAdd(TemplateView): de
我在 get_or_create 调用中使用 icontains 得到了意外结果。 举个例子: >>>team_name = "Bears" >>>Team.objects.get(name__ico
我想插入几个User数据库中的行。我真的不在乎插入是否成功,只要我得到通知,在这两种情况下我都能做到,那么哪一个在性能(主要是速度)方面更好? 始终插入行(通过调用模型的 save 方法)并捕获潜在的
我一直在 Django 应用程序中使用 get_or_create 方法和 MongoEngine。今天,我注意到有一些重复的条目。我在 get_or_create 的 MongoEngine API
我一直在 Django 应用程序中使用 get_or_create 方法和 MongoEngine。今天,我注意到有一些重复的条目。我在 get_or_create 的 MongoEngine API
我在 get_or_create 语句方面遇到问题。如果有记录,它会获取记录但不创建记录。我的所有模型字段都有默认值。 我的观点.py from django.contrib.auth.dec
考虑以下(伪Python)代码: l = [some, list] for i in l: o, c = Model.objects.get_or_create(par1=i["somethi
我的 postgresql 数据库中有一个表,其中包含一个小时记录的状态。对于每个月、项目和用户,我只需要一个状态。我正在使用 get_or_create 方法来创建“状态”或检索它(如果它已经存在)
我有一个像这样的 ExtendedUser 模型,它只指向 Django 的 User 模型: class ExtendedUser(models.Model): user = models.
我必须使用具有相似字段的表,并且我想将对象从一个表复制到另一个表。第二个表中可能没有对象的问题,所以我必须使用 get_or_create() 方法: #these are new products,
我正在尝试对表单中的某些字段使用 get_or_create,但在尝试这样做时出现 500 错误。 其中一行看起来像这样: customer.source = Source.objects.get_o
假设您正在像这样修改管理器中的 get_queryset class VoteManager(models.Manager): def get_queryset(self):
我是一名优秀的程序员,十分优秀!