Code

Opened 2 years ago

Closed 2 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.

Attachments (0)

Change History (1)

comment:1 Changed 2 years ago by claudep

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

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.

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.