django核心代码片段:
class ForeignKey(RelatedField, Field):
...
def db_type(self, connection):
rel_field = self.rel.get_related_field()
if (isinstance(rel_field, AutoField) or
(not connection.features.related_fields_match_type and
isinstance(rel_field, (PositiveIntegerField,
PositiveSmallIntegerField)))):
return IntegerField().db_type(connection=connection)
return rel_field.db_type(connection=connection)
这段代码非常糟糕,因为如果我定义一个继承自AutoField
的自定义字段,那么我的db_type方法将被忽略。
我想要隐藏的事实是我的班级是AutoField
的一个实例。在C ++中,我会通过私有继承来实现。
有没有办法欺骗isinstance
返回False
或隐藏继承?
我的cutom字段代码:
class MyAutoField(models.AutoField):
def __init__(self, length, *args, **kwargs):
self.length = length
super(MyAutoField, self).__init__(*args, **kwargs)
def db_type(self, connection):
if connection.vendor == 'oracle':
return 'NUMBER(%s,0)' % (self.length)
if connection.vendor == 'postgresql':
if self.length <= 4:
return 'smallint'
if self.length <= 9:
return 'integer'
return 'bigint'
return super(MyAutoField, self).db_type(connection)
答案 0 :(得分:5)
所以我有办法做到这一点,但它通过“猴子修补”。您不能使用ABCMeta
,因为这需要覆盖基类的元类。
您可以“更改”实例如下。重要的是“补丁”只能导入一次,我只会这样做,如果没有别的办法。
patches.py
import django.models
import mymodel
import __builtin__
def _isinstance(instance, clz):
if clz is models.AutoField and isinstance_orig(instance, MyAutoField):
return False
return isinstance_orig(instance, clz)
__builtin__.isinstance_orig = __builtin__.isinstance
__builtin__.isinstance = _isinstance
然后你的测试程序:
class MyAutoField(models.AutoField): pass
x = MyAutoField()
print(isinstance(x, models.AutoField))
print(isinstance(x, models.MyAutoField))
介绍抽象基类:PEP-3119。以下是一个抽象的例子......
class ABCMeta(type):
def __instancecheck__(cls, inst):
"""Implement isinstance(inst, cls)."""
return any(cls.__subclasscheck__(c)
for c in {type(inst), inst.__class__})
def __subclasscheck__(cls, sub):
"""Implement issubclass(sub, cls)."""
candidates = cls.__dict__.get("__subclass__", set()) | {cls}
return any(c in candidates for c in sub.mro())