Skip to main content
https://travis-ci.org/iwoca/django-deep-collector.svg?branch=master

Django custom collector used to get every objects that are related to given object.

Install

$ pip install django-deep-collector

Usage

Create a new instance of DeepCollector, and launch collector on one object:

from deep_collector.core import DeepCollector
from django.contrib.auth.models import User

user = User.objects.all()[0]
collector = DeepCollector()
collector.collect(user)
related_objects = collector.get_collected_objects()

If you want to save it in a file to be ‘django load_data’-like imported, you can use:

string_buffer = collector.get_json_serialized_objects()

How it works

This class is used to introspect an object, to get every other objects that depend on it, following its ‘relation’ fields, i.e. ForeignKey, OneToOneField, ManyToManyField, GenericForeignKey and GenericRelation.

  1. We start from given object (of class classA for example), and loop over :

  • Its ‘direct’ fields, it means the relation fields that are explicitly declared in this django model.

class classA(models.Model):
    fkey = models.ForeignKey(classB)
    o2o = models.OneToOneField(classC)
    m2m = models.ManyToManyField(classD)
  • Its ‘related’ fields, so other django model that are related to this object by relation fields.

class classB(models.Model):
    fkeyto = models.ForeignKey(classA)

class classC(models.Model):
    o2oto = models.OneToOneField(classA)

class classD(models.Model):
    m2mto = models.ManyToManyField(classA)
  1. For every field, we get associated object(s) of objA:

  • If it’s a direct field, we get objects by:

class classA(models.Model):
    fkey = models.ForeignKey(classB)        # objA.fkey
    o2o = models.OneToOneField(classC)      # objA.o2o
    m2m = models.ManyToManyField(classD)    # objA.m2m.all()
  • If it’s a related field, we get objects by:

class classB(models.Model):
    fkeyto = models.ForeignKey(classA)      # objA.classb_set.all()

class classC(models.Model):
    o2oto = models.OneToOneField(classA)    # objA.classC (not a manager, because OneToOneField is a unique rel)

class classD(models.Model):
    m2mto = models.ManyToManyField(classA)  # objA.classd_set.all()

If we are using related_name attribute, then we access manager with its related_name:

class classE(models.Model):
    m2mto = models.ForeignKey(classA, related_name='classE')  # objA.classE.all()
  1. For each associated object, we go back to step 1. and get every field, …

GenericForeignKey

The GenericForeignKey has a small exception. If you want it to be collected in the “reverse” way, you should explicitly define a GenericRelation in the models you want to follow this “reverse” relation.

class GFKModel(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')


class BaseToGFKModel(models.Model):
    gfk_relation = GenericRelation(GFKModel)

In above example, if you collect a BaseToGFKModel instance, the collector will look for all GFKModel instances related to your initial BaseToGFKModel instance. That happens because the BaseToGFKModel model explicitly defines a GenericRelation.

Parameters

You can customize which model/field is collected. By default, every model and field is collected, but you can override some parameters to have custom behaviour:

  • EXCLUDE_MODELS: exclude models (expecting a list of ‘<app_label>.<module_name>’)

EXCLUDE_MODELS = ['sites.site', 'auth.permission', 'auth.group']

Every time we will try to collect an object of this model type, it won’t be collected.

  • EXCLUDE_DIRECT_FIELDS: exclude direct fields from specified models

EXCLUDE_DIRECT_FIELDS = {
    'auth.user': ['groups'],
}

On User model, when we will get direct fields, we won’t take into account ‘groups’ field.

  • EXCLUDE_RELATED_FIELDS: exclude related fields from specified models

EXCLUDE_RELATED_FIELDS = {
    'auth.user': ['session_set']
}

On User model, we don’t want to collect sessions that are associated to this user, so we put the exact accessor name we have to use to get these sessions, ‘session_set’, to exclude them from collection.

  • ALLOWS_SAME_TYPE_AS_ROOT_COLLECT: avoid by default to collect objects that have the same type as the root one, to prevent collecting too many data.

Miscellaneous

To avoid some recursive collect between 2 objects (if an object has a direct field to another one, it means that other object has a related field to this first one), we detect if an object has already been collected before trying to collect it.

Metadata

Release files for django-deep-collector 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-deep-collector 0.6.0
File Size Uploaded Provenance Actions
django-deep-collector-0.6.0.tar.gz 11.7 kB Without provenance Details for django-deep-collector-0.6.0.tar.gz

Release files / django-deep-collector-0.6.0.tar.gz

Download URL django-deep-collector-0.6.0.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
03fca5b100e9a1da3ff73c0e423ebd05114353c97d4a9c3259e34d249f144640
BLAKE2b-256 checksum
How to use checksums
a8cceaf7e3c12d7109fd751e896cfdcdd0ecb2ef0c64b34559d6f01d93a88462
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.7

Security for django-deep-collector 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page