I have tags which can be associated with images and locations.
- Tags are unique.
- Images and locations can have many tags.
- A Tag_Item model is used to link everything together.
Here are the models:
LOCATIONS = (
('US', 'USA'),
('UK', 'United Kingdom'),
('FR', 'France'),
)
class Location(models.Model):
location = models.CharField(choices=LOCATIONS)
class Image(models.Model):
image = models.ImageField(verbose_name='Image')
class Tag(models.Model):
tag = models.CharField(max_length=150, unique=True)
class Tag_Item(models.Model):
tag = models.ForeignKey(Tag, on_delete=models.CASCADE)
location = models.ForeignKey(Location, null=True, blank=True, default=None)
image = models.ForeignKey(Image, null=True, blank=True, default=None)
created_at = models.DateTimeField(auto_now_add=True)
I want to write a query which means select the five most frequent tags for USA.
I was thinking something along the following lines in SQL:
- Join Tag, Tag_Item, and Location where location is 'US'.
- Group it by tag.
- Order it by a count of Tag_ID (or something along those lines).
but I can't figure out how to transfer that in Django ORM.
Can you please help me on how to write that sort of complex relationship queries?