Contact Us
Back

Draup Django

Technology Django January 3, 2019




Draup Django
Ganesh Subramaniam
Ganesh Subramaniam

Share

At Draup, we take problem-solving to the next level by using cutting-edge technologies and having some of the brightest minds running highly complex algorithms on huge volumes of data. While we have the capabilities to solve most challenges, we also rely on open-sourcing which equips us with the right tools which allows us to find truly unique and innovate solutions to several problems.

The team at Draup has been trying to solve the problem of stateful deletion/updation at the ORM level for Django. Whilst solving the problem, we also wanted to build a generic solution for ORM, so it could be used by Django-developers as well.

Stateful Deletion/Updation :

Stateful deletion is when an object cannot be directly deleted, without a prior prompt message (when deleting a single object entity, the prompt message gesticulates all the other dependent objects tagged along with the parent entity, before deleting it).

Stateful updation occurs while transferring object dependencies from one object entity to another object entity, through which duplicate entries from database-tables can be erased.

Draup Django :

Draup Django is primarily a Pip Package. It provides stateful deletion/updation and is, available here.

Installation :

pip install draup-django

Utilities :

Get Affected Objects :

  • Lists out all the dependent objects(Prompt-Messages).
  • getAffectedObjects takes input dict with id as key and reference of the model.
   

   Sample Input : ({‘id’:1},model_reference)
   Output : Error_list(list),prompt messages(list)

Delete Object :

  • Deletes an object with all the other dependent objects.
  • deleteObject takes input dict with id, and force_delete as key and reference of the model.
  

  Sample Input : ({‘id’:1,’force_delete’:True},model_reference)
  Output : Error_list(list)

Update Object :

  • Transfers object dependencies from one object to another object.
  • updateObjectDependencies takes an input source, and destination objects.
  

  Sample Input : (source,destination)
  Output : Error_list(list)

Usage :

  • Sample model :
 
    
    class parent(models.Model):
        name = models.CharField(max_length=100)    
    class child(models.Model):
        parent = models.ForeignKey(parent, on_delete=models.CASCADE)
        name = models.CharField(max_length=100)

  • Parent Table :
   

   ╔═══════════╦════════════════════╗
   ║   Id      ║       Name         ║      
   ╠═══════════╬════════════════════╣
   ║ 1         ║     Alice          ║ 
   ║ 2         ║     Tom            ║ 
   ╚═══════════╩════════════════════╝

  • Child Table :
   

   ╔═══════════╦════════════════════╦═══════════════╗
   ║   id      ║       Name         ║   parent_id   ║
   ╠═══════════╬════════════════════╬═══════════════╣
   ║ 1         ║        Bob         ║     1         ║
   ║ 2         ║        Jack        ║     2         ║
   ╚═══════════╩════════════════════╩═══════════════╝

  • Code :
    
    from draup_django import utility 
    m = models.parent
    """
    Get Prompt Message 
    """
    error_list,prompt_message = utility.getAffectedObjects({‘id’:1},
    m)
    """
    Deletion with all dependent objects
    """
    error_list = utility.deleteObject({‘id’:1,’force_delete’:True},
    m)
    """
    Transferring Object dependencies
    """
    source = m.objects.filter(id=1).first()
    destination = m.objects.filter(id=2).first()
    error_list = utility.updateObjectDependencies(source,
    destination)

  • Output : Prompt Message of getAffectedObjects function
   
 
    [{'message': 'This object has been used in child 1 times.',
          'model_name':'child','Parent models': '', 'count': 1}]

Note :

  • Every function returns the error_list, and hence only the operation is successful only if it is empty.
  • According to the above child/parent table, the updateObjectDependencies function will transfer the object dependency from id:1 to id:2, so the parent_id of Bob will become 2.
  • Updated Child Table :
    ╔═══════════╦════════════════════╦═══════════════╗
    ║   id      ║       Name         ║   parent_id   ║ 
    ╠═══════════╬════════════════════╬═══════════════╣
    ║ 1         ║        Bob         ║     2         ║
    ║ 2         ║        Jack        ║     2         ║
    ╚═══════════╩════════════════════╩═══════════════╝

Limitation :

  • updateObjectDependencies will not work in case of a unique constraint, and one-to-one field in the model.
  • Feel free to share your thoughts and feedback to keep adding more such utilities. Reach out to us at info@draup.com.

About Draup :

Draup is an enterprise decision-making platform for global CXO leaders in sales and talent domains. Draup combines Artificial Intelligence with human curation to help organizations make data-driven strategic decisions. The platform is powered by machine- generated models, which are augmented by a team of analysts adding their learning-based insights to provide a 360-degree transactable view of their sales and talent ecosystem.