I am having an issue coming up with a way to analyze data that I've been 
collecting. I would rather not change my model definition at this point, 
but I am open to suggestions. 

A Bundle is a group of audio/video/AC cables that are taped together in our 
warehouse to our client's specification.  We build hundreds of these per 
month and would like to analyze the most common Bundles that we create to 
increase efficiency.

Here is my model definition:

class Bundle(models.Model):
    name = models.CharField(max_length=50)
    length = models.CharField(max_length=50, choices=LENGTH)
    created = models.DateField(auto_now=True)
    archived = models.BooleanField(default=False)

class Cable(models.Model):
    cable_type = models.CharField(max_length=50, choices=CABLE_TYPES)
    origin_cable_connector = models.CharField(
            max_length=50, choices=CONNECTORS, verbose_name='Taped End')
    destination_cable_connector = models.CharField(
            max_length=50, choices=CONNECTORS, verbose_name='Non-taped End')
    bundle = models.ForeignKey('Bundle')

Some examples for the constants:
    CABLE_TYPES: powercon, 19pr, XLR
    CONNECTORS: powercon(blue), powercon(grey), G3(male), G3(female), 
XLR(Male), XLR(Female), 
    LENGTH: 25ft, 150ft, 200ft

If I wanted to know the number of times a similar Bundle is created, I 
wanted to use something like:

repeated_sets = {}
all_cable_sets = [list(sorted(b.cable_set.all())) for b in 
Bundle.objects.all()]

for set in all_cable_sets:
    occurences = all_cable_sets.count(set)
    print occurences
    if occurences > 1:
        if set not in repeated_sets:
            repeated_sets[set] = occurences

This doesn't work because each set belongs to a different bundle, so they 
are all unique and occurences never goes greater than 1.

I would also like to know the lengths of these similar Bundles and the 
number of times identical bundles were created where the cable_sets are all 
the same and their parent Bundle's length is the same.  I figured I should 
attempt the simpler query first without the length, and add length later by 
iterating through the repeated_sets dictionary and querying for each 
cable_set's parent Bundle and generating a new dictionary out of that. 

Should I be approaching this from a different angle?  Is there a nice 
Django way of making these comparisons and counts?

Thank you,

-Matt

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to