Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#10453 closed (fixed)

get_db_prep_value called with seralized data

Reported by: brian Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Hello,

In order to resolve the difficulties encountered by ticket #10028, I installed the latest code from the http://code.djangoproject.com/svn/django/branches/releases/1.0.X branch.

It appears that the behaviour of the "get_db_prep_value" function has changed, and the current behaviour seems wrong to me.

(note: actual function call is get_db_prep_save, but as this just calls get_db_prep_value I consider them the same)

From:

<http://docs.djangoproject.com/en/dev/howto/custom-model-fields/>

def get_db_prep_value(self, value):
        return ''.join([''.join(l) for l in (value.north,
                value.east, value.south, value.west)])

This would imply value should always be the python object returned by to_python.

It seems in my case, when calling save() on an object and this field is not updated, then value contains the raw un-serialized string data straight from the database.

I could work around this by checking if the value is a string, and if so, returning it as is, but to me this seems wrong.

Stack trace:

Traceback (most recent call last):
  File "./import", line 681, in <module>
    sys.exit(main())
  File "./import", line 663, in main
    host.process_all(force)
  File "./import", line 438, in process_all
    self.update_last_seen(self.datetime)
  File "/home/brian/myproject/lintory/inventory/importer.py", line 160, in update_last_seen
    self.computer.save()
  File "/home/brian/tree/django/django/db/models/base.py", line 329, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
  File "/home/brian/tree/django/django/db/models/base.py", line 380, in save_base
    rows = manager.filter(pk=pk_val)._update(values)
  File "/home/brian/tree/django/django/db/models/query.py", line 434, in _update
    query.add_update_fields(values)
  File "/home/brian/tree/django/django/db/models/sql/subqueries.py", line 239, in add_update_fields
    val = field.get_db_prep_save(val)
  File "/home/brian/tree/django/django/db/models/fields/__init__.py", line 192, in get_db_prep_save
    return self.get_db_prep_value(value)
  File "/home/brian/myproject/lintory/names.py", line 188, in get_db_prep_value
    return value.get_value()
AttributeError: 'str' object has no attribute 'get_value'

From this stack trace, it would appear that get_db_prep_save is called twice for the same data:

  • Line 379 of django/db/models/base.py
  • Line 239 of db/models/sql/subqueries.py

Brian May

Attachments (0)

Change History (2)

comment:1 Changed 5 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to fixed
  • Status changed from new to closed

I'm thinking this is probably fixed by r10013 (trunk) and r10014 (1.0.X). If not, can you please reopen with a step-by-step to repeating the problem (preferably a patch against the tests that fail, but if not, at least something we can repeat).

comment:2 Changed 5 years ago by brian

Yes, just to confirm, it is fixed now. Thanks.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.