0
class Room(models.Model):
assigned_floor = models.ForeignKey(Floor, null=True, on_delete=models.CASCADE)
room_nr = models.CharField(db_index=True, max_length=4, unique=True, null=True)
locked = models.BooleanField(db_index=True, default=False)
last_cleaning = models.DateTimeField(db_index=True, auto_now_add=True, null=True)
...
class Floor(models.Model):
assigned_building = models.ForeignKey(Building, on_delete=models.CASCADE)
wall_color = models.CharField(db_index=True, max_length=255, blank=True, null=True)
...
class Building(models.Model):
name = models.CharField(db_index=True, max_length=255, unique=True, null=True)
number = models.PositiveIntegerField(db_index=True)
color = models.CharField(db_index=True, max_length=255, null=True)
...
我要输出所有客房均可通过Building.number排序的表格。 数据,我想打印每间客房: Building.number,Building.color,Building.name,Floor.wall_color,Room.last_cleaning
而且我想允许可选的过滤器: Room.locked室。最后清理Floor.wall_color Building.number Building.color
有一张桌子对我来说不是问题,但我不知道如何将它归档到三张桌子上。
kwargs = {'number': 123}
kwargs['color'] = 'blue'
all_buildings = Building.objects.filter(**kwargs).order_by('-number')
你能帮我吗?我是否需要编写原始SQL查询,还是可以使用Django模型查询API对它进行归档?
我使用最新的Django版本和PostgreSQL。