django save() 拒绝更新现有记录
django save() refuses to update an existing record
我有下面的模型和我从 view.py 调用的函数来更新该模型中名为 unread_count 的字段。但是,它一直尝试创建记录而不是更新现有记录,我收到如下所示的错误。我已经包含了 2 个打印语句来显示记录存在。我已经尝试了各种方法来让它工作,但我没有取得任何进展(除了扯掉我的头发)。任何帮助将不胜感激。
class RoomOccupier(TimeStampedModel):
room = models.ForeignKey(Room, on_delete=models.CASCADE, db_index=True)
occupier = models.ForeignKey(UserAccount, on_delete=models.CASCADE, related_name="+", db_index=True)
unread_count = models.PositiveSmallIntegerField(default=0, blank=False)
def __int__(self): # __unicode__ for Python 2
return self
@database_sync_to_async
def increment_unread(room_id):
roomOccupiers = RoomOccupier.objects.filter(room_id=room_id)
print(roomOccupiers)
for roomOccupier in roomOccupiers:
print(roomOccupier)
roomOccupier.unread_count += 1
roomOccupier.save()
return true
<QuerySet [<RoomOccupier: RoomOccupier object (1)>, <RoomOccupier: RoomOccupier object (2)>]>
RoomOccupier object (1)
Exception inside application: NOT NULL constraint failed: chat_roomoccupier.created
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: chat_roomoccupier.created
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/channels/staticfiles.py", line 44, in __call__
return await self.application...etc...
我建议在 bulk 中更新查询集 .update(…)
[Django-doc]:
from django.db.models import F
@database_sync_to_async
def increment_unread(room_id):
RoomOccupier.objects.filter(room_id=room_id)<strong>.update(</strong>
<strong>unread_count=F('unread_count')+1</strong>
<strong>)</strong>
return True
我有下面的模型和我从 view.py 调用的函数来更新该模型中名为 unread_count 的字段。但是,它一直尝试创建记录而不是更新现有记录,我收到如下所示的错误。我已经包含了 2 个打印语句来显示记录存在。我已经尝试了各种方法来让它工作,但我没有取得任何进展(除了扯掉我的头发)。任何帮助将不胜感激。
class RoomOccupier(TimeStampedModel):
room = models.ForeignKey(Room, on_delete=models.CASCADE, db_index=True)
occupier = models.ForeignKey(UserAccount, on_delete=models.CASCADE, related_name="+", db_index=True)
unread_count = models.PositiveSmallIntegerField(default=0, blank=False)
def __int__(self): # __unicode__ for Python 2
return self
@database_sync_to_async
def increment_unread(room_id):
roomOccupiers = RoomOccupier.objects.filter(room_id=room_id)
print(roomOccupiers)
for roomOccupier in roomOccupiers:
print(roomOccupier)
roomOccupier.unread_count += 1
roomOccupier.save()
return true
<QuerySet [<RoomOccupier: RoomOccupier object (1)>, <RoomOccupier: RoomOccupier object (2)>]>
RoomOccupier object (1)
Exception inside application: NOT NULL constraint failed: chat_roomoccupier.created
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: chat_roomoccupier.created
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/channels/staticfiles.py", line 44, in __call__
return await self.application...etc...
我建议在 bulk 中更新查询集 .update(…)
[Django-doc]:
from django.db.models import F
@database_sync_to_async
def increment_unread(room_id):
RoomOccupier.objects.filter(room_id=room_id)<strong>.update(</strong>
<strong>unread_count=F('unread_count')+1</strong>
<strong>)</strong>
return True