Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#10453 closed (fixed)

get_db_prep_value called with seralized data

Reported by: Brian May 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: no UI/UX: no

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

Change History (2)

comment:1 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

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 by Brian May, 16 years ago

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

Note: See TracTickets for help on using tickets.
Back to Top