[Django] #22450: Error when model.save(force_insert=True)

7 views
Skip to first unread message

Django

unread,
Apr 15, 2014, 7:21:21 PM4/15/14
to django-...@googlegroups.com
#22450: Error when model.save(force_insert=True)
----------------------------------------------+----------------------------
Reporter: vcajes@… | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer (models, ORM) | Version: 1.6
Severity: Normal | Keywords: model save
Triage Stage: Unreviewed | force_insert
Easy pickings: 0 | Has patch: 0
| UI/UX: 0
----------------------------------------------+----------------------------
I'm having an error when I add the force_insert=True parameter to a
model.save call. The problem is arising in a Django Unit TestCase (from
django.test import TestCase)

The model example:

{{{
class Company(models.Model):
owner = models.ForeignKey(User)
name = models.CharField(max_length=75)
description = models.CharField(max_length=250)
created = models.DateTimeField()
status = models.CharField(max_length=1,
choices=COMPANY_STATUS_CHOICES, default='a')

def save(self, *args, **kwargs):
if not self.pk:
self.created = timezone.now()
super(Company, self).save(args, kwargs)
}}}

The code error example:

{{{
self.company = Company(owner=self.operator, name="Company1",
description="Descripcion Company1", status='a')
self.company.save(force_insert=True)
}}}

And the error say it cannot force an '''UPDATE''' (I think there are some
parameters that are passed in a wrong order somewhere)


{{{
Error
Traceback (most recent call last):
File "C:\Users\KGs\Documents\Coupoints\wsgi\openshift\points\tests.py",
line 36, in setUp
self.company.save(force_insert=True)
File
"C:\Users\KGs\Documents\Coupoints\wsgi\openshift\business\models.py", line
49, in save
super(Company, self).save(args, kwargs)
File "C:\VirtualEnvs\CoupointsEnv\lib\site-
packages\django\db\models\base.py", line 545, in save
force_update=force_update, update_fields=update_fields)
File "C:\VirtualEnvs\CoupointsEnv\lib\site-
packages\django\db\models\base.py", line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update,
using, update_fields)
File "C:\VirtualEnvs\CoupointsEnv\lib\site-
packages\django\db\models\base.py", line 626, in _save_table
raise ValueError("Cannot force an update in save() with no primary
key.")
ValueError: Cannot force an update in save() with no primary key.
}}}

The problem is in here:

In the file django.db.models.base.py in the function:

{{{
def _save_table(self, raw=False, cls=None, force_insert=False,
force_update=False, using=None, update_fields=None):
}}}

If you print the parameters force_insert, force_update, update_fields just
before the:

{{{
if not pk_set and (force_update or update_fields):
raise ValueError("Cannot force an update in save() with no
primary key.")
}}}

You will see that force_insert is None and force_update has the value
{'force_insert': True}, and thats the problem.

Its an easy to solve problem. Y just wanted to report. Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/22450>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 15, 2014, 11:58:29 PM4/15/14
to django-...@googlegroups.com
#22450: Error when model.save(force_insert=True)
-------------------------------------+-------------------------------------
Reporter: vcajes@… | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 1.6
(models, ORM) | Resolution: invalid
Severity: Normal | Triage Stage:
Keywords: model save | Unreviewed
force_insert | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by charettes):

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

You actually made an error when overriding `Model.save()`, you're not
correctly passing over `args` and `kwargs`.


{{{#!python


def save(self, *args, **kwargs):
if not self.pk:
self.created = timezone.now()

super(Company, self).save(*args, **kwargs)
}}}

Notice the `*` before `args` and the double one before `kwargs`.

See TicketClosingReasons/UseSupportChannels.

--
Ticket URL: <https://code.djangoproject.com/ticket/22450#comment:1>

Reply all
Reply to author
Forward
0 new messages