Opened 10 years ago

Closed 10 years ago

#23703 closed Bug (wontfix)

django.db.models.field.subclassing overwrites my subclass

Reported by: mozumder Owned by: nobody
Component: Uncategorized Version: 1.7
Severity: Normal 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

In the file subclassing.py, should the following:


def make_contrib(superclass, func=None):

"""
Returns a suitable contribute_to_class() method for the Field subclass.

If 'func' is passed in, it is the existing contribute_to_class() method on
the subclass and it is called before anything else. It is assumed in this
case that the existing contribute_to_class() calls all the necessary
superclass methods.
"""
def contribute_to_class(self, cls, name):

if func:

func(self, cls, name)

else:

super(superclass, self).contribute_to_class(cls, name)

setattr(cls, self.name, Creator(self))

return contribute_to_class


(note the setattr() is outside of the if func.)

instead be:


def make_contrib(superclass, func=None):

"""
Returns a suitable contribute_to_class() method for the Field subclass.

If 'func' is passed in, it is the existing contribute_to_class() method on
the subclass and it is called before anything else. It is assumed in this
case that the existing contribute_to_class() calls all the necessary
superclass methods.
"""
def contribute_to_class(self, cls, name):

if func:

func(self, cls, name)

else:

super(superclass, self).contribute_to_class(cls, name)
setattr(cls, self.name, Creator(self))

return contribute_to_class


(note the setattr() is inside of the if func.)

Should the setattr() should be called only when there isn’t a contribute_to_class() method.

I ask because in my own custom field, I have a contribute_to_class() function as well:


def contribute_to_class(self, cls, name):

super(myCustomField, self).contribute_to_class(cls, name)
setattr(cls, self.name, myCustomCreator(self))


But, because of the setattr() in the SubfieldBase metaclass, my own setattr() gets overwritten by the default set_attr().. I therefore can’t have my own field of the same name with my own custom Creator().

Is this expected behavior? I would like to be able to produce my own Creator() function.

-bobby

Change History (1)

comment:1 by Tim Graham, 10 years ago

Resolution: wontfix
Status: newclosed

SubfieldBase is deprecated in Django 1.8, so I don't think there will be interest in fixing any issues with it.

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