Ticket #15250: fill_parent_model_instance_1.diff
File fill_parent_model_instance_1.diff, 4.1 KB (added by , 14 years ago) |
---|
-
db/models/base.py
9 9 from django.core import validators 10 10 from django.db.models.fields import AutoField, FieldDoesNotExist 11 11 from django.db.models.fields.related import (OneToOneRel, ManyToOneRel, 12 OneToOneField, add_lazy_relation )12 OneToOneField, add_lazy_relation, ReverseSingleRelatedObjectDescriptorParent) 13 13 from django.db.models.query import Q 14 14 from django.db.models.query_utils import DeferredAttribute 15 15 from django.db.models.deletion import Collector … … 155 155 elif not is_proxy: 156 156 attr_name = '%s_ptr' % base._meta.module_name 157 157 field = OneToOneField(base, name=attr_name, 158 auto_created=True, parent_link=True)158 auto_created=True, rev_class = ReverseSingleRelatedObjectDescriptorParent, parent_link=True) 159 159 new_class.add_to_class(attr_name, field) 160 160 else: 161 161 field = None -
db/models/fields/related.py
374 374 # object you just set. 375 375 setattr(instance, self.field.get_cache_name(), value) 376 376 377 class ReverseSingleRelatedObjectDescriptorParent(ReverseSingleRelatedObjectDescriptor): 378 def __get__(self, instance, instance_type=None): 379 if instance is None: 380 return self 381 382 cache_name = self.field.get_cache_name() 383 try: 384 return getattr(instance, cache_name) 385 except AttributeError: 386 kwargs = {} 387 for field in self.field.rel.to._meta.fields: 388 if isinstance(field.rel, ManyToOneRel): 389 key = field.attname 390 else: 391 key = field.name 392 kwargs[key] = getattr(instance, key) 393 rel_obj = self.field.rel.to(**kwargs) 394 setattr(instance, cache_name, rel_obj) 395 return rel_obj 396 377 397 class ForeignRelatedObjectsDescriptor(object): 378 398 # This class provides the functionality that makes the related-object 379 399 # managers available as attributes on a model class, for fields that have … … 815 835 'invalid': _('Model %(model)s with pk %(pk)r does not exist.') 816 836 } 817 837 description = _("Foreign Key (type determined by related field)") 818 def __init__(self, to, to_field=None, rel_class=ManyToOneRel, **kwargs):838 def __init__(self, to, to_field=None, rel_class=ManyToOneRel, rev_class = ReverseSingleRelatedObjectDescriptor, **kwargs): 819 839 try: 820 840 to_name = to._meta.object_name.lower() 821 841 except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT … … 837 857 parent_link=kwargs.pop('parent_link', False), 838 858 on_delete=kwargs.pop('on_delete', CASCADE), 839 859 ) 860 self.rev_class = rev_class 840 861 Field.__init__(self, **kwargs) 841 862 842 863 def validate(self, value, model_instance): … … 889 910 890 911 def contribute_to_class(self, cls, name): 891 912 super(ForeignKey, self).contribute_to_class(cls, name) 892 setattr(cls, self.name, ReverseSingleRelatedObjectDescriptor(self))913 setattr(cls, self.name, self.rev_class(self)) 893 914 if isinstance(self.rel.to, basestring): 894 915 target = self.rel.to 895 916 else: … … 939 960 rather than returning a list. 940 961 """ 941 962 description = _("One-to-one relationship") 942 def __init__(self, to, to_field=None, **kwargs):963 def __init__(self, to, to_field=None, rev_class = ReverseSingleRelatedObjectDescriptor, **kwargs): 943 964 kwargs['unique'] = True 944 super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs)965 super(OneToOneField, self).__init__(to, to_field, OneToOneRel, rev_class, **kwargs) 945 966 946 967 def contribute_to_related_class(self, cls, related): 947 968 setattr(cls, related.get_accessor_name(),