I have a Django model that looks roughly like this
class Equipment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=100)
class Tag(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True)
tag_type = models.ForeignKey(TagType)
equipment = models.ForeignKey(Equipment)
name = models.CharField(max_length=100)
class Meta:
db_table = "tag"
ordering = ["tag_type__name", "name"]
unique_together = (("tag_type", "equipment"),)
class TagType(models.Model):
id = models.PositiveSmallIntegerField(primary_key=True)
name = models.CharField(max_length=100, unique=True)
class Meta:
db_table = "tag_type"
ordering = ["name"]
TagTypes have names such as "Equipment" and "Load"
"Equipment" tags can be "HVAC" or "Lighting"
"Load" tags can be "Variable" or "Weather"
I want to be able to filter to equipment that have a certain tag_type with a tag name.
If I want equipment that have an HVAC tag with tag_type Equipment the following works:
Equipment.objects.filter(tag__tag_type__name="Equipment", tag__name="HVAC")
However, I believe that in this case the tag_type name and the tag aren't necessarily from the same tag. AKA tag__tag_type__name="Equipment" and tag__name="HVAC" might not be referring with the right values.
Since there can be many different types of tags with diverse names, I want to make sure I'm filtering to the right one. Is there a way I can manage this?