I have a set of four models representing rental properties.
The Property model stores the property's name and who created it.
class Property(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
creator = models.ForeignKey(User, related_name='creator', on_delete=models.PROTECT)
name = models.CharField(max_length=100)
A Property can have many Area instances throug a ForeignKey relationship. An Area has an AreaType through a ForeignKey relationship as well, and many Amenities through a ManyToManyField.
class Area(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
type = models.ForeignKey(AreaType, on_delete=models.PROTECT)
property = models.ForeignKey(Property, on_delete=models.PROTECT)
amenities = models.ManyToManyField(Amenity, null=True, blank=True)
class AreaType(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=100)
class Amenity(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=100)
I am familiar with field lookups that do and do not span relationships, as well as filtering through kwargs.
What I am not familiar with is combining field lookups and aggregation functions. And therefore, given the models above, I would like to:
- Retrieve all Properties that have at least 3 Amenities with the name 'Television'.
- Retrieve all Properties that have at least 2 Areas with an AreaType name of 'Bathroom'.