Consider the following hierarchical data. It can be 3 to 4 levels deep. I understand I can use existing packages like django-mptt or django-treebeard to create a tree data structure.
- Mercedes
- Consumer
- ModelA
- ModelB
- SUV
- ModelC
- Luxury
- ModelD
- Consumer
- Nissan
- Consumer
- ModelA
- ModelB
- SUV
- ModelC
- Consumer
Now let's say I have another model called Battery. A battery can be compatible with multiple models for different market segments by different car vendors. So what I want to do is assign this battery to one or more compatible models above. I'm not sure how to accomplish this linkage in Django. Would it just be a ManytoMany field to the hierarchical model? Some pseudo-code would help.
class Battery(models.Model)
name = Charfield(max_length=50)
compatible_models = ????
I would also like to know how a query would be written. For example, I want to query all the battery that are compatible with ModelA by Mercedes, etc.
I think the below would work for you (in pure Django):
You would query Batteries as follows: