Opened 13 years ago

Closed 13 years ago

#18031 closed Bug (wontfix)

Subclassing a model field: call_with_connection_and_prepared bug

Reported by: smbrooks1@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords: Custom Field, get_db_prep_value
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A custom model Field class whose get_db_prep_value method calls super(..., self).get_db_prep_value(value, connection, prepared) sees an exception raised in django.db.models.fields.subclassing on line 53 in inner.
TypeError: get_db_prep_value() got multiple values for keyword argument 'connection'.

It is happening because the 'inner' function of 'call_with_connection_and_prepared' looks for 'connection' in kwargs, and if not there, it adds it. However, in this case, it is present as a positional argument - hence the duplication.

NOTE: The 'call_with_connection' has a similar construction and most likely suffers from the same problem.

To reproduce:

from django.db import models

class MyField(models.CharField):
    def get_db_prep_value(self, value, connection, prepared=False):
        return super(MyField, self).get_db_prep_value(value, connection, prepared)

Then define a model using this field, create an instance and try to save it.

class MyModel(models.Model):
    myfield = MyField()

mm = MyModel()
mm.myfield = 'test'
mm.save()

If you enable DeprecationWarnings, you will see that the DeprecationWarning issued on line 49 of subclassing, in the 'inner' function, is displayed.

Change History (1)

comment:1 by Claude Paroz, 13 years ago

Resolution: wontfix
Status: newclosed

Thanks for the report, but this is now old code, and unless there are serious security implications, I'm afraid we won't touch it any more. Sorry. Reopen if you can reproduce the bug with recent code.

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