mirror of https://github.com/django/django.git
Fixed #21940 -- Added kwargs to contribute_to_class() of model fields..
Thanks Kronuz for the suggestion.
This commit is contained in:
parent
a0bfe4ea98
commit
1ed6fbcf44
|
@ -28,7 +28,7 @@ class GenericForeignKey(object):
|
||||||
self.for_concrete_model = for_concrete_model
|
self.for_concrete_model = for_concrete_model
|
||||||
self.editable = False
|
self.editable = False
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.model = cls
|
self.model = cls
|
||||||
self.cache_attr = "_%s_cache" % name
|
self.cache_attr = "_%s_cache" % name
|
||||||
|
@ -316,8 +316,9 @@ class GenericRelation(ForeignObject):
|
||||||
qs = getattr(obj, self.name).all()
|
qs = getattr(obj, self.name).all()
|
||||||
return smart_text([instance._get_pk_val() for instance in qs])
|
return smart_text([instance._get_pk_val() for instance in qs])
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
super(GenericRelation, self).contribute_to_class(cls, name, virtual_only=True)
|
kwargs['virtual_only'] = True
|
||||||
|
super(GenericRelation, self).contribute_to_class(cls, name, **kwargs)
|
||||||
# Save a reference to which model this class is on for future use
|
# Save a reference to which model this class is on for future use
|
||||||
self.model = cls
|
self.model = cls
|
||||||
# Add the descriptor for the relation
|
# Add the descriptor for the relation
|
||||||
|
|
|
@ -210,8 +210,8 @@ class GeometryField(Field):
|
||||||
return gsrid
|
return gsrid
|
||||||
|
|
||||||
### Routines overloaded from Field ###
|
### Routines overloaded from Field ###
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
super(GeometryField, self).contribute_to_class(cls, name)
|
super(GeometryField, self).contribute_to_class(cls, name, **kwargs)
|
||||||
|
|
||||||
# Setup for lazy-instantiated Geometry object.
|
# Setup for lazy-instantiated Geometry object.
|
||||||
setattr(cls, self.attname, GeometryProxy(Geometry, self))
|
setattr(cls, self.attname, GeometryProxy(Geometry, self))
|
||||||
|
|
|
@ -914,10 +914,10 @@ class AutoField(Field):
|
||||||
return None
|
return None
|
||||||
return int(value)
|
return int(value)
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
assert not cls._meta.has_auto_field, \
|
assert not cls._meta.has_auto_field, \
|
||||||
"A model can't have more than one AutoField."
|
"A model can't have more than one AutoField."
|
||||||
super(AutoField, self).contribute_to_class(cls, name)
|
super(AutoField, self).contribute_to_class(cls, name, **kwargs)
|
||||||
cls._meta.has_auto_field = True
|
cls._meta.has_auto_field = True
|
||||||
cls._meta.auto_field = self
|
cls._meta.auto_field = self
|
||||||
|
|
||||||
|
@ -1226,8 +1226,8 @@ class DateField(DateTimeCheckMixin, Field):
|
||||||
else:
|
else:
|
||||||
return super(DateField, self).pre_save(model_instance, add)
|
return super(DateField, self).pre_save(model_instance, add)
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
super(DateField, self).contribute_to_class(cls, name)
|
super(DateField, self).contribute_to_class(cls, name, **kwargs)
|
||||||
if not self.null:
|
if not self.null:
|
||||||
setattr(cls, 'get_next_by_%s' % self.name,
|
setattr(cls, 'get_next_by_%s' % self.name,
|
||||||
curry(cls._get_next_or_previous_by_FIELD, field=self,
|
curry(cls._get_next_or_previous_by_FIELD, field=self,
|
||||||
|
|
|
@ -300,8 +300,8 @@ class FileField(Field):
|
||||||
file.save(file.name, file, save=False)
|
file.save(file.name, file, save=False)
|
||||||
return file
|
return file
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
super(FileField, self).contribute_to_class(cls, name)
|
super(FileField, self).contribute_to_class(cls, name, **kwargs)
|
||||||
setattr(cls, self.name, self.descriptor_class(self))
|
setattr(cls, self.name, self.descriptor_class(self))
|
||||||
|
|
||||||
def get_directory_name(self):
|
def get_directory_name(self):
|
||||||
|
@ -407,8 +407,8 @@ class ImageField(FileField):
|
||||||
kwargs['height_field'] = self.height_field
|
kwargs['height_field'] = self.height_field
|
||||||
return name, path, args, kwargs
|
return name, path, args, kwargs
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
super(ImageField, self).contribute_to_class(cls, name)
|
super(ImageField, self).contribute_to_class(cls, name, **kwargs)
|
||||||
# Attach update_dimension_fields so that dimension fields declared
|
# Attach update_dimension_fields so that dimension fields declared
|
||||||
# after their corresponding image field don't stay cleared by
|
# after their corresponding image field don't stay cleared by
|
||||||
# Model.__init__, see bug #11196.
|
# Model.__init__, see bug #11196.
|
||||||
|
|
|
@ -2263,7 +2263,7 @@ class ManyToManyField(RelatedField):
|
||||||
data = [choices_list[0][0]]
|
data = [choices_list[0][0]]
|
||||||
return smart_text(data)
|
return smart_text(data)
|
||||||
|
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
# To support multiple relations to self, it's useful to have a non-None
|
# To support multiple relations to self, it's useful to have a non-None
|
||||||
# related name on symmetrical relations for internal reasons. The
|
# related name on symmetrical relations for internal reasons. The
|
||||||
# concept doesn't make a lot of sense externally ("you want me to
|
# concept doesn't make a lot of sense externally ("you want me to
|
||||||
|
@ -2273,7 +2273,7 @@ class ManyToManyField(RelatedField):
|
||||||
if self.rel.symmetrical and (self.rel.to == "self" or self.rel.to == cls._meta.object_name):
|
if self.rel.symmetrical and (self.rel.to == "self" or self.rel.to == cls._meta.object_name):
|
||||||
self.rel.related_name = "%s_rel_+" % name
|
self.rel.related_name = "%s_rel_+" % name
|
||||||
|
|
||||||
super(ManyToManyField, self).contribute_to_class(cls, name)
|
super(ManyToManyField, self).contribute_to_class(cls, name, **kwargs)
|
||||||
|
|
||||||
# The intermediate m2m model is not auto created if:
|
# The intermediate m2m model is not auto created if:
|
||||||
# 1) There is a manually specified intermediate, or
|
# 1) There is a manually specified intermediate, or
|
||||||
|
|
|
@ -46,11 +46,11 @@ def make_contrib(superclass, func=None):
|
||||||
case that the existing contribute_to_class() calls all the necessary
|
case that the existing contribute_to_class() calls all the necessary
|
||||||
superclass methods.
|
superclass methods.
|
||||||
"""
|
"""
|
||||||
def contribute_to_class(self, cls, name):
|
def contribute_to_class(self, cls, name, **kwargs):
|
||||||
if func:
|
if func:
|
||||||
func(self, cls, name)
|
func(self, cls, name, **kwargs)
|
||||||
else:
|
else:
|
||||||
super(superclass, self).contribute_to_class(cls, name)
|
super(superclass, self).contribute_to_class(cls, name, **kwargs)
|
||||||
setattr(cls, self.name, Creator(self))
|
setattr(cls, self.name, Creator(self))
|
||||||
|
|
||||||
return contribute_to_class
|
return contribute_to_class
|
||||||
|
|
Loading…
Reference in New Issue