Esempio n. 1
0
    def test_with_non_rel_field(self):
        """Testing get_field_is_relation with non-relation field"""
        # Test with a normal field.
        self.assertFalse(
            get_field_is_relation(CompatModelsAnchor._meta.get_field('value')))

        # Test with a primary key.
        self.assertFalse(
            get_field_is_relation(CompatModelsAnchor._meta.get_field('id')))
Esempio n. 2
0
    def test_with_many_to_many_field_rel(self):
        """Testing get_field_is_relation with ManyToManyField relation"""
        rel = get_remote_field(
            CompatModelsTestModel._meta.get_field('m2m_field'))

        self.assertIsInstance(rel, related.ManyToManyRel)
        self.assertTrue(get_field_is_relation(rel))
Esempio n. 3
0
    def test_with_foreign_key_rel(self):
        """Testing get_field_is_relation with ForeignKey relation"""
        rel = get_remote_field(
            CompatModelsTestModel._meta.get_field('fkey_field'))

        self.assertIsInstance(rel, related.ManyToOneRel)
        self.assertTrue(get_field_is_relation(rel))
Esempio n. 4
0
    def test_with_one_to_one_field_rel(self):
        """Testing get_field_is_relation with OneToOneField relation"""
        rel = get_remote_field(
            CompatModelsTestModel._meta.get_field('o2o_field'))

        self.assertIsInstance(rel, related.ManyToOneRel)
        self.assertTrue(get_field_is_relation(rel))
Esempio n. 5
0
def get_model_rel_tree():
    """Return the full field relationship tree for all registered models.

    This will walk through every field in every model registered in Django,
    storing the relationships between objects, caching them. Each entry in
    the resulting dictionary will be a table mapping to a list of relation
    fields that point back at it.

    This can be used to quickly locate any and all reverse relations made to
    a field.

    This is similar to Django's built-in reverse relation tree used internally
    (with different implementations) in
    :py:class:`django.db.models.options.Options`, but works across all
    supported versions of Django, and supports cache clearing.

    Version Added:
        2.2

    Returns:
        dict:
        The model relation tree.
    """
    global _rel_tree_cache

    if _rel_tree_cache is not None:
        return _rel_tree_cache

    rel_tree = defaultdict(list)
    all_models = get_models(include_auto_created=True)

    # We'll walk the entire model tree, looking for any immediate fields on
    # each model, building a mapping of models to fields that reference the
    # model.
    for cur_model in all_models:
        if cur_model._meta.abstract:
            continue

        for field in iter_model_fields(cur_model,
                                       include_parent_models=False,
                                       include_forward_fields=True,
                                       include_reverse_fields=False,
                                       include_hidden_fields=False):
            if (get_field_is_relation(field)
                    and get_remote_field_related_model(field) is not None):
                remote_field = get_remote_field(field)
                remote_field_model = get_remote_field_model(remote_field)

                # Make sure this isn't a "self" relation or similar.
                if not isinstance(remote_field_model, six.string_types):
                    db_table = \
                        remote_field_model._meta.concrete_model._meta.db_table
                    rel_tree[db_table].append(field)

    _rel_tree_cache = rel_tree

    return rel_tree
Esempio n. 6
0
 def test_with_one_to_one_field(self):
     """Testing get_field_is_relation with OneToOneField"""
     self.assertTrue(
         get_field_is_relation(
             CompatModelsTestModel._meta.get_field('o2o_field')))
Esempio n. 7
0
 def test_with_many_to_many_field(self):
     """Testing get_field_is_relation with ManyToManyField"""
     self.assertTrue(
         get_field_is_relation(
             CompatModelsTestModel._meta.get_field('m2m_field')))
Esempio n. 8
0
 def test_with_foreign_key(self):
     """Testing get_field_is_relation with ForeignKey"""
     self.assertTrue(
         get_field_is_relation(
             CompatModelsTestModel._meta.get_field('fkey_field')))