Beispiel #1
0
    def _prepare(self, model):
        if self.order_with_respect_to:
            self.order_with_respect_to = self.get_field(
                self.order_with_respect_to)
            self.ordering = ('_order', )
            model.add_to_class('_order', OrderWrt())
        else:
            self.order_with_respect_to = None

        if self.pk is None:
            if self.parents:
                # Promote the first parent link in lieu of adding yet another
                # field.
                field = next(six.itervalues(self.parents))
                # Look for a local field with the same name as the
                # first parent link. If a local field has already been
                # created, use it instead of promoting the parent
                already_created = [
                    fld for fld in self.local_fields if fld.name == field.name
                ]
                if already_created:
                    field = already_created[0]
                field.primary_key = True
                self.setup_pk(field)
            else:
                auto = AutoField(verbose_name='ID',
                                 primary_key=True,
                                 auto_created=True)
                model.add_to_class('id', auto)
Beispiel #2
0
    def _prepare(self, model):
        if self.order_with_respect_to:
            # The app registry will not be ready at this point, so we cannot
            # use get_field().
            query = self.order_with_respect_to
            try:
                self.order_with_respect_to = next(
                    f for f in self._get_fields(reverse=False)
                    if f.name == query or f.attname == query
                )
            except StopIteration:
                raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, query))

            self.ordering = ('_order',)
            if not any(isinstance(field, OrderWrt) for field in model._meta.local_fields):
                model.add_to_class('_order', OrderWrt())
        else:
            self.order_with_respect_to = None

        if self.pk is None:
            if self.parents:
                # Promote the first parent link in lieu of adding yet another
                # field.
                field = next(iter(self.parents.values()))
                # Look for a local field with the same name as the
                # first parent link. If a local field has already been
                # created, use it instead of promoting the parent
                already_created = [fld for fld in self.local_fields if fld.name == field.name]
                if already_created:
                    field = already_created[0]
                field.primary_key = True
                self.setup_pk(field)
            else:
                auto = AutoField(verbose_name='ID', primary_key=True, auto_created=True)
                model.add_to_class('id', auto)
Beispiel #3
0
    def _prepare(self, model):
        # 排序的属性
        if self.order_with_respect_to:
            self.order_with_respect_to = self.get_field(self.order_with_respect_to)

            self.ordering = ('_order',)
            model.add_to_class('_order', OrderWrt())
        else:
            self.order_with_respect_to = None

        if self.pk is None:
            if self.parents:
                # Promote 促进, 提升 the first parent link in lieu of adding yet another
                # field.
                field = next(six.itervalues(self.parents))

                # Look for a local field with the same name as the
                # first parent link. If a local field has already been
                # created, use it instead of promoting the parent
                already_created = [fld for fld in self.local_fields if fld.name == field.name]

                if already_created:
                    field = already_created[0]

                field.primary_key = True
                self.setup_pk(field)
            else:
                auto = AutoField(verbose_name='ID', primary_key=True,
                        auto_created=True)
                model.add_to_class('id', auto)

        # 如果两个属性同时关联两个外部表, 比如两个外键的时候, 需要创建一个集合.
        # Determine any sets of fields that are pointing to the same targets
        # (e.g. two ForeignKeys to the same remote model). The query
        # construction code needs to know this. At the end of this,
        # self.duplicate_targets will map each duplicate field column to the
        # columns it duplicates.
        collections = {}
        for column, target in six.iteritems(self.duplicate_targets):

            try:
                collections[target].add(column)
            except KeyError:
                collections[target] = set([column])

        self.duplicate_targets = {}
        for elt in six.itervalues(collections):
            if len(elt) == 1:
                continue

            for column in elt:
                # elt_set - set([column])
                self.duplicate_targets[column] = elt.difference(set([column]))
Beispiel #4
0
 def init_name_map(self):
     """
     Initialises the field name -> field object mapping.
     """
     cache = {}
     # We intentionally handle related m2m objects first so that symmetrical
     # m2m accessor names can be overridden, if necessary.
     for f, model in self.get_all_related_m2m_objects_with_model():
         cache[f.field.related_query_name()] = (f, model, False, True)
     for f, model in self.get_all_related_objects_with_model():
         cache[f.field.related_query_name()] = (f, model, False, False)
     for f, model in self.get_m2m_with_model():
         cache[f.name] = (f, model, True, True)
     for f, model in self.get_fields_with_model():
         cache[f.name] = (f, model, True, False)
     if self.order_with_respect_to:
         cache['_order'] = OrderWrt(), None, True, False
     if app_cache_ready():
         self._name_map = cache
     return cache
Beispiel #5
0
    def _prepare(self, model):
        if self.order_with_respect_to:
            self.order_with_respect_to = self.get_field(
                self.order_with_respect_to)
            self.ordering = ('_order', )
            model.add_to_class('_order', OrderWrt())
        else:
            self.order_with_respect_to = None

        if self.pk is None:
            if self.parents:
                # Promote the first parent link in lieu of adding yet another
                # field.
                field = self.parents.value_for_index(0)
                field.primary_key = True
                self.setup_pk(field)
            else:
                auto = AutoField(verbose_name='ID',
                                 primary_key=True,
                                 auto_created=True)
                model.add_to_class('id', auto)

        # Determine any sets of fields that are pointing to the same targets
        # (e.g. two ForeignKeys to the same remote model). The query
        # construction code needs to know this. At the end of this,
        # self.duplicate_targets will map each duplicate field column to the
        # columns it duplicates.
        collections = {}
        for column, target in self.duplicate_targets.iteritems():
            try:
                collections[target].add(column)
            except KeyError:
                collections[target] = set([column])
        self.duplicate_targets = {}
        for elt in collections.itervalues():
            if len(elt) == 1:
                continue
            for column in elt:
                self.duplicate_targets[column] = elt.difference(set([column]))