Login

models.py with django_dag models for parts hierarchy

Author:
j_syk
Posted:
August 3, 2011
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

This is a portion of my code for creating a hierarchical relation between assemblies, subparts, and parts and so forth (although really, everything is a Part)

The project being used is django_dag

For the sake of example, let's use this structure:

  • Bike 1
    • Front Tire & Back Tire combo 000
      • Front Tire 000
      • Rear Tire 000
    • Frame 000
    • Gearset type 000
      • Crank 000
      • Rear Cassette 000
  • .
  • Bike 2
    • Front Tire & Back Tire combo 001
      • Front Tire 001
      • Rear Tire 000
    • Frame 001
    • Gearset type 000
      • Crank 000
      • Rear Cassette 000

Using the above example, I couldn't use a MPTT structure because Gearset 000 is used in 2 different parents (bike 1 and bike 2). So I had to use the DAG (wiki) structure which allows multiple parents.

The Relationship model holds the dag information, mainly a parent and child field. (Tire combo 001 is child of Bike2, front tire 001 is child of tire combo 001, etc) After get the dag structure using the ancestors_tree method on a Part object, I search for the BillOfMaterial info for that whole tree.

In my case the Bill Of Material info is unique per assembly, so that's why there are seperate models.

It becomes a bit of a pain to have to save/delete both the relationship and BoM models at the same time, and to check if one exists to create the other etc. But I've made my first 'hack' through it to have a function project, and am ready to make some revisions now.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from django.db import models
from django_dag.models import *
from django_dag import models as dag

Relationship=dag.edge_factory('parts.Part') #must specify appname (in this case: 'parts')

class Part(dag.node_factory(Relationship)):
    part_no = models.CharField(max_length=30, unique=True, db_index=True)
    description = models.TextField(null=True, blank=True)
    date_modified = models.DateTimeField(auto_now=True, auto_now_add=False, null=True, blank=True,)

    def __unicode__(self):
	return u'%s' % (self.part_no)

    class Meta:
        ordering = ['-date_modified']

class BillOfMaterial(models.Model):
    assembly = models.ForeignKey(Part, related_name='bom_assembly', db_index=False)
    subpart = models.ForeignKey(Part, related_name='bom_subpart', db_index=False)
    item = models.IntegerField(max_length=10, null=True, blank=True)
    qty = models.DecimalField(max_digits=10, decimal_places=5)
    order = models.IntegerField(max_length=10, null=True, blank=True)
    depth = models.IntegerField(max_length=10, null=True, blank=True)
 
    def __unicode__(self):
        return u'A:%s P:%s' % (self.assembly_no, self.subpart_no)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.