Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

367
Views
django save() se niega a actualizar un registro existente

Tengo el siguiente modelo y una función a la que llamo desde view.py para actualizar un campo dentro de ese modelo llamado unread_count. Sin embargo, sigue intentando crear un registro en lugar de actualizar el existente y aparece el error que se muestra a continuación. He incluido 2 declaraciones de impresión para mostrar que existen registros. He intentado varias cosas para que funcione, pero no estoy haciendo ningún progreso (además de arrancarme los pelos). Cualquier ayuda sería apreciada.

 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...
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Aconsejaría actualizar el querset de forma masiva , con .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) .update( unread_count=F('unread_count')+1 ) return True
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!