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。