I've got two models. One represents a piece of equipment, the other represents a possible attribute the equipment has. Semantically, this might look like:
- Equipment:
tractor
, Attributes:wheels
,towing
- Equipment:
lawnmower
, Attributes:wheels
,blades
- Equipment:
hedgetrimmer
, Attributes:blades
I want to make queries like,
wheels = Attributes.objects.get(name='wheels')
blades = Attributes.objects.get(name='blades')
Equipment.objects.filter(has_attribute=wheels) \
.exclude(has_attribute=blades)
How can I create Django models to do this?
This seems simple, but I'm just too dense to see the right solution.
One solution that popped into my head is to encode the list of Attribute
IDs in an integer list like |109|14|3
and test for attributes using Equipment.objects.filter(attributes_contains='|%d|' % id)
-- but this seems really wrong.