1
\$\begingroup\$

How can my solution be improved / optimized?

explaination of db models :
thinkpad is a equipment of laptop subcategory.and laptop subcategory has a category called electronics.
now laptop can have many attributes like processor,ram,color,screen_size.

Question :
Find out all the equipments with theirequipment detail ,subcategory_name and attribute names,value which have category_id =1 .

Desired Output:

[ 
   { 
      'equipment_id':1,
      'subcategory__name':'laptop',
      'attribute_detail':[ 
         { 
            'attribute_name':'color',
            'attribute_value':'red'
         },
         { 
            'attribute_name':'ram',
            'attribute_value':'6gb'
         }
      ]
   },
   {   'equipment_id':2,
      'subcategory__name':'Mouse',
      'attribute_detail':[ 
         { 
            'attribute_name':'color',
            'attribute_value':'red'
         },
         { 
            'attribute_name':'dpi',
            'attribute_value':'800'
         }
      ]
    }

]  

Solution:

equipments = Equipment.objects.filter(subcategory__category__category_id=1)  


all_equipments_data = []  

# iterate over each equipment  

for equipment in equipments:
    single_equipment_data = {}  
    single_equipment_data['equipment_id'] = equipment.equipment_id
    single_equipment_data['subcategory__name'] = equipment.subcategory.subcategory_name  

    attributes_detail_of_equipment = []  

# iterate over each equipmentdetail of single equipment

    for eqdetail in equipment.equipment_eqdetail.all(): 
        single_attribute_detail_of_equipment = {}  
        single_attribute_detail_of_equipment['attribute_name'] = eqdetail.attribute.attribute_name
        single_attribute_detail_of_equipment['attribute_value'] = eqdetail.value  

        attributes_detail_of_equipment.append(single_attribute_detail_of_equipment)  

        single_equipment_data['attribute_detail'] = attributes_detail_of_equipment  

all_equipments_data.append(single_equipment_data)

Models :

class Category(models.Model):
    category_id = models.AutoField(primary_key=True, default=None)
    category_name = models.CharField(max_length=15, unique=True)
    created_at = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.category_id)+","+self.category_name

    class Meta:
        db_table = "category"


class Subcategory(models.Model):
    subcategory_id = models.AutoField(primary_key=True, default=None)
    category = models.ForeignKey(
        Category, on_delete=models.CASCADE, related_name="category_subc", verbose_name="category_id")
    subcategory_name = models.CharField(max_length=15, unique=True)
    created_at = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.subcategory_id)+","+self.subcategory_name

    class Meta:
        db_table = "subcategory"


class Equipment(models.Model):
    equipment_id = models.AutoField(primary_key=True, default=None)
    subcategory = models.ForeignKey(
        Subcategory, on_delete=models.CASCADE, related_name="subcategory_eq", verbose_name="subcategory_id")
    created_at = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.equipment_id)

    class Meta:
        db_table = "equipment"


class Attribute(models.Model):
    attribute_id = models.AutoField(primary_key=True, default=None)
    attribute_name = models.CharField(max_length=15, unique=True)
    subcategory = models.ManyToManyField(
        Subcategory, through="SubcategoryAttributeMap")
    created_at = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.attribute_id)+","+self.attribute_name

    class Meta:
        db_table = "attribute"


class SubcategoryAttributeMap(models.Model):
    id = models.AutoField(primary_key=True, default=None)
    subcategory = models.ForeignKey(
        Subcategory, on_delete=models.CASCADE, related_name="subcategory_sub_attr_map", verbose_name="subcategory_id")
    attribute = models.ForeignKey(
        Attribute, on_delete=models.CASCADE, related_name="attribute_sub_attr_map", verbose_name="attribute_id")
    created_at = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.id)

    class Meta:
        db_table = "subcategory_attribute_map"


class EquipmentDetail(models.Model):
    equipment_detail_id = models.AutoField(primary_key=True, default=None)
    equipment = models.ForeignKey(
        Equipment, on_delete=models.CASCADE, related_name="equipment_eqdetail", verbose_name="equipment_id")
    attribute = models.ForeignKey(
        Attribute, on_delete=models.CASCADE, related_name="attribute_eqdetail", verbose_name="attribute_id")
    value = models.CharField(max_length=15)
    created_at = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.equipment_detail_id)+","+self.value

    class Meta:
        db_table = "equipment_detail"
\$\endgroup\$
  • 1
    \$\begingroup\$ Hey Naveen, please edit your title to state what your code does. Asking questions in the title makes it difficult for users to be interested in the question. \$\endgroup\$ – IEatBagels Oct 2 '19 at 18:32
  • \$\begingroup\$ @IEatBagels i have updated the title , but i dont understand why downvote when everything seems proper \$\endgroup\$ – Naveen Jain Oct 2 '19 at 21:59
  • 2
    \$\begingroup\$ Don't worry about a single downvote. Your post is good, it'll attract the votes and the answers :) \$\endgroup\$ – IEatBagels Oct 3 '19 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.