class StorageUnit(models.Model):
"""
id_ - Auto generated unique identifier
name - Unique String for name of the storage unit
num_racks - Integer for number of racks
num_subracks - Integer for number of subracks per rack
num_boxes - Integer for number of boxes per subracks
num_rows - Integer for number of rows per box
num_positions - Integer for number of positions per row
"""
id_ = models.AutoField(primary_key=True)
name = models.CharField(max_length=100, blank=True, unique=True)
num_racks = models.PositiveSmallIntegerField()
num_subracks = models.PositiveSmallIntegerField()
num_boxes = models.PositiveSmallIntegerField()
num_rows = models.PositiveSmallIntegerField()
num_positions = models.PositiveSmallIntegerField()
def __str__(self):
return str(self.name)
def create_positions(self):
for rack in range(1, self.num_racks + 1):
for subrack in range(1, self.num_subracks + 1):
for box in range(1, self.num_boxes + 1):
for row in range(1, self.num_rows + 1):
for position in range(1, self.num_positions + 1):
p = StoragePosition(
storage_unit=self,
rack=rack,
subrack=subrack,
box=box,
row=row,
position=position
)
p.save()
def save(self, *args, **kwargs):
super(StorageUnit, self).save(*args, **kwargs)
self.create_positions()
class StoragePosition(models.Model):
"""
id_ - Auto generated unique identifier
name - Unique string for name of the storage position
storage_unit - Foreign key to StorageUnit
rack - Integer for rack number
subrack - Integer for subrack number
box - Integer for box number
row - Integer for row number
position - Integer for position number
"""
id_ = models.AutoField(primary_key=True)
name = models.CharField(max_length=100, blank=True, unique=True)
storage_unit = models.ForeignKey(StorageUnit)
rack = models.PositiveSmallIntegerField()
subrack = models.PositiveSmallIntegerField()
box = models.PositiveSmallIntegerField()
row = models.PositiveSmallIntegerField()
position = models.PositiveSmallIntegerField()
def __str__(self):
return str(self.name)
class Meta:
ordering = ['id_']
def save(self, *args, **kwargs):
self.name = (
f'{self.storage_unit}_'
f'R{self.rack}_'
f'{self.subrack}_'
f'{chr(self.box + 96).upper()}_' # Because chr(97) == 'a' and so on
f'R{self.row}P{self.position}'
)
super(StoragePosition, self).save(*args, **kwargs)
class Sample(models.Model):
"""
id_ - Auto generated unique identifier
storage_position - Position of the sample in the storage unit
"""
id_ = models.AutoField(primary_key=True)
storage_position = models.OneToOneField(
'StoragePosition',
null=True,
blank=True,
default=None,
help_text='The position where the sample is to be stored'
)
def __str__(self):
return str(self.id_)
我现在打算做的是计算不包含任何样本的存储位置块。
代表我的意思的模型:
class FreeStorageBlock(models.Model):
"""
id_ - Auto generated unique identifier
slots - Number of free storage position slots
start_storage_position - First free storage position in block
end_storage_position - Last free storage position in block
"""
id_ = models.AutoField(primary_key=True)
slots = models.PositiveSmallIntegerField()
start_storage_position = models.ForeignKey(StoragePosition)
end_storage_position = models.ForeignKey(StoragePosition)
def __str__(self):
return str(self.id_)
我的问题是:
维持这种关系形式的正确方法是什么?
我什至应该建立一个这样的模型吗?
我还能做些什么来生成从其他3个模型派生的列表吗?
是否有一些搜索词供我阅读?
当样品分配到存储位置并且可用存储块发生更改(数量和尺寸均发生变化)时,是否有一种方法可以自动使此状态保持最新?