我正在尝试创建一个查询来查找与一组平面图中至少一个匹配的租用者,并且需要帮助编写查询。
Renter模型
class RenterProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, blank=True)
name = models.CharField(max_length=120)
desired_location = models.CharField(max_length=120)
move_in_date = models.DateField(null=True, blank=True, help_text='Use mm/dd/yyyy format')
coordinates =models.CharField(max_length=120, blank=True, null=True)
lat = models.CharField(max_length=120, blank=True, null=True)
lng = models.CharField(max_length=120, blank=True, null=True)
price = models.IntegerField(max_length=120, blank=True, null=True)
numbers = (('0','0'),('1','1'),('2','2'),('3','3'),('4','4'),('5','5'),('6+','6+'),)
beds = models.CharField(max_length=120, blank=True, null=True, choices=numbers)
baths = models.CharField(max_length=120, blank=True, null=True, choices=numbers)
FloorPlan模型
class FloorPlan(models.Model):
property_name = models.ForeignKey(Property)
floor_plan_name = models.CharField(max_length=120, blank=True, null=True)
numbers = (('0','0'),('1','1'),('2','2'),('3','3'),('4','4'),('5','5'),('6+','6+'),)
bedrooms = models.CharField(max_length=120, blank=True, null=True, choices=numbers)
bathrooms = models.CharField(max_length=120, blank=True, null=True, choices=numbers)
sqft = models.IntegerField(max_length=120, blank=True, null=True)
min_price = models.IntegerField(max_length=120, blank=True, null=True)
max_price = models.IntegerField(max_length=120, blank=True, null=True)
availability = models.DateField(null=True, blank=True, help_text='Use mm/dd/yyyy format')
image = models.ImageField(upload_to='floor_plans/', null=True, blank=True)
答案 0 :(得分:0)
您可以制作如下功能:
from django.db.models import Q
def get_floor_plan(renter_profile):
'''
It will take RenterProfile model class's object as input.
It will return a FloorPlan Queryset with desired matches of properties
'''
return FloorPlan.objects.filter(Q(bedrooms=renter_profile.beds)|(Q(maxprice__lte=renter_profile.price)&Q(min_price__gte=renter_profile.price))|Q(availability__lte=renter_profile.move_in_date))