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|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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.
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.