Tool to iterate over django relation tree
Project description
django-relations-Iterator
Provides utilities for iterating over django model instances hierarchy
##Example:
Simple instances tree clone
#models.py
from django.conf import settings
from django.db import models
class Meeting(models.Model):
title = models.CharField(max_length=200)
time = models.DateTimeField(null=True, blank=True)
participants = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Participation', blank=True)
class Participation(models.Model):
meeting = models.ForeignKey('Meeting', on_delete=models.CASCADE, related_name='participations')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='participations')
class Invitation(models.Model):
STATUS_SENT = 'sent'
STATUS_ACCEPTED = 'accepted'
STATUS_DECLINED = 'declined'
STATUS_CHOICES = (
(STATUS_SENT, STATUS_SENT),
(STATUS_ACCEPTED, STATUS_ACCEPTED),
(STATUS_DECLINED, STATUS_DECLINED),
)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default=STATUS_SENT)
participation = models.ForeignKey('Participation', on_delete=models.CASCADE, related_name='invitations')
class Comment(models.Model):
meeting = models.ForeignKey('Meeting', on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
description = models.TextField(max_length=3000)
#clone.py
from relations_iterator import TreeNode, AbstractVisitor, RelationTreeIterator, ConfigurableRelationTree
from .models import Meeting
# because of config, tree will ignore comments, but will consider all participations and invitations
CLONE_STRUCTURE = {
'participations': {
'invitations': {}
}
}
class CloneVisitor(AbstractVisitor):
def visit(self, node: TreeNode):
node.instance.pk = None
if node.parent is not None:
parent_joining_column, instance_joining_column = node.relation.get_joining_columns()[0]
setattr(
node.instance,
instance_joining_column,
getattr(node.parent.instance, parent_joining_column)
)
node.instance.save()
def clone(instance, config):
visitor = CloneVisitor()
tree = ConfigurableRelationTree(root=instance, structure=config)
for node in RelationTreeIterator(tree=tree):
visitor.visit(node)
meeting = Meeting.objects.last()
clone(meeting, CLONE_STRUCTURE)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django-relations-iterator-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fc300c8e4a1fd986d35a77bcf25c84e75540b8ef83e6c75c75d6d751b2c96d5 |
|
MD5 | a565a50bf0709f3c7abb96c1b9a0cbd7 |
|
BLAKE2b-256 | 78e88424623a593c0aecaa3a7838d533528e8ef1ae36490ee5c6f7ffc6772f12 |
Close
Hashes for django_relations_iterator-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56a7c26af043b73ad87082a1ff81776da0eef813087baa0338d162dec0112747 |
|
MD5 | 4482f0d11e906f1d294d617f30ad9d40 |
|
BLAKE2b-256 | c54cf4b386a22b902a5a409acbd28a64eb14645bde5fe3b78f2ebd937db4f55a |