Commit c339a5a6 authored by Michal Petrucha's avatar Michal Petrucha Committed by Tim Graham
Browse files

Refs #16508 -- Renamed the current "virtual" fields to "private".

The only reason why GenericForeignKey and GenericRelation are stored
separately inside _meta is that they need to be cloned for every model
subclass, but that's not true for any other virtual field. Actually,
it's only true for GenericRelation.
parent 47fbbc33
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ class GenericInlineModelAdminChecks(InlineModelAdminChecks):
        # and that they are part of a GenericForeignKey.

        gfks = [
            f for f in obj.model._meta.virtual_fields
            f for f in obj.model._meta.private_fields
            if isinstance(f, GenericForeignKey)
        ]
        if len(gfks) == 0:
+3 −3
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ class GenericForeignKey(object):
        self.name = name
        self.model = cls
        self.cache_attr = "_%s_cache" % name
        cls._meta.add_field(self, virtual=True)
        cls._meta.add_field(self, private=True)

        # Only run pre-initialization field assignment on non-abstract models
        if not cls._meta.abstract:
@@ -331,7 +331,7 @@ class GenericRelation(ForeignObject):
    def _check_generic_foreign_key_existence(self):
        target = self.remote_field.model
        if isinstance(target, ModelBase):
            fields = target._meta.virtual_fields
            fields = target._meta.private_fields
            if any(isinstance(field, GenericForeignKey) and
                    field.ct_field == self.content_type_field_name and
                    field.fk_field == self.object_id_field_name
@@ -409,7 +409,7 @@ class GenericRelation(ForeignObject):
        return smart_text([instance._get_pk_val() for instance in qs])

    def contribute_to_class(self, cls, name, **kwargs):
        kwargs['virtual_only'] = True
        kwargs['private_only'] = True
        super(GenericRelation, self).contribute_to_class(cls, name, **kwargs)
        self.model = cls
        setattr(cls, self.name, ReverseGenericManyToOneDescriptor(self.remote_field))
+3 −3
Original line number Diff line number Diff line
@@ -161,7 +161,7 @@ class ModelBase(type):
        new_fields = chain(
            new_class._meta.local_fields,
            new_class._meta.local_many_to_many,
            new_class._meta.virtual_fields
            new_class._meta.private_fields
        )
        field_names = {f.name for f in new_fields}

@@ -268,9 +268,9 @@ class ModelBase(type):
            if is_proxy:
                new_class.copy_managers(original_base._meta.concrete_managers)

            # Inherit virtual fields (like GenericForeignKey) from the parent
            # Inherit private fields (like GenericForeignKey) from the parent
            # class
            for field in base._meta.virtual_fields:
            for field in base._meta.private_fields:
                if base._meta.abstract and field.name in field_names:
                    raise FieldError(
                        'Local field %r in class %r clashes '
+2 −2
Original line number Diff line number Diff line
@@ -147,7 +147,7 @@ class Collector(object):
        for related in get_candidate_relations_to_delete(opts):
            if related.field.remote_field.on_delete is not DO_NOTHING:
                return False
        for field in model._meta.virtual_fields:
        for field in model._meta.private_fields:
            if hasattr(field, 'bulk_related_objects'):
                # It's something like generic foreign key.
                return False
@@ -221,7 +221,7 @@ class Collector(object):
                        self.fast_deletes.append(sub_objs)
                    elif sub_objs:
                        field.remote_field.on_delete(self, field, sub_objs, self.using)
            for field in model._meta.virtual_fields:
            for field in model._meta.private_fields:
                if hasattr(field, 'bulk_related_objects'):
                    # It's something like generic foreign key.
                    sub_objs = field.bulk_related_objects(new_objs, self.using)
+17 −3
Original line number Diff line number Diff line
@@ -672,11 +672,25 @@ class Field(RegisterLookupMixin):
        if self.verbose_name is None and self.name:
            self.verbose_name = self.name.replace('_', ' ')

    def contribute_to_class(self, cls, name, virtual_only=False):
    def contribute_to_class(self, cls, name, private_only=False, virtual_only=NOT_PROVIDED):
        """
        Register the field with the model class it belongs to.

        If private_only is True, a separate instance of this field will be
        created for every subclass of cls, even if cls is not an abstract
        model.
        """
        if virtual_only is not NOT_PROVIDED:
            warnings.warn(
                "The `virtual_only` argument of Field.contribute_to_class() "
                "has been renamed to `private_only`.",
                RemovedInDjango20Warning, stacklevel=2
            )
            private_only = virtual_only
        self.set_attributes_from_name(name)
        self.model = cls
        if virtual_only:
            cls._meta.add_field(self, virtual=True)
        if private_only:
            cls._meta.add_field(self, private=True)
        else:
            cls._meta.add_field(self)
        if self.choices:
Loading