Python Django模型:从模型实例中的其他字段生成字段内容
例如,假设我正在记录库存项目,我需要每个项目的唯一标识符(单独读取时也是有意义的) 下面显示了我正在尝试的模型Python Django模型:从模型实例中的其他字段生成字段内容,python,django,database,Python,Django,Database,例如,假设我正在记录库存项目,我需要每个项目的唯一标识符(单独读取时也是有意义的) 下面显示了我正在尝试的模型 class InventoryItem(models.Model): item_name = models.TextField(max_length = 100) item_manufacturer = models.TextField(max_length = 20) item_product_num = models.TextField(max_length
class InventoryItem(models.Model):
item_name = models.TextField(max_length = 100)
item_manufacturer = models.TextField(max_length = 20)
item_product_num = models.TextField(max_length = 25)
item_lot = models.TextField(max_length = 25)
item_received_on = models.DateTimeField(auto_now_add = True)
item_price = models.DecimalField()
item_quantity = models.DecimalField()
item_special_instructions = models.TextField(default = "NA", max_length = 200)
item_reinspection_date = models.DateField()
def makeUniqueID(self):
unique_str = self.item_manufacturer + self.item_product_num + self.item_lot + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
return unique_str
item_uniqueID = models.TextField(max_length = 50, default = self.makeUniqueID())
上面的代码错误NameError:name“self”没有定义,我怀疑这不是正确的方法。
非常感谢您的帮助
我使用的数据库是SQLite,如果它改变了任何内容,请尝试以下操作:
class InventoryItem(models.Model):
item_name = models.TextField(max_length = 100)
item_manufacturer = models.TextField(max_length = 20)
item_product_num = models.TextField(max_length = 25)
item_lot = models.TextField(max_length = 25)
item_received_on = models.DateTimeField(auto_now_add = True)
item_price = models.DecimalField()
item_quantity = models.DecimalField()
item_special_instructions = models.TextField(default = "NA", max_length = 200)
item_reinspection_date = models.DateField()
item_uniqueID = models.TextField(max_length = 50)
def save(self, *args, **kwargs):
if not self.item_uniqueID:
self.item_uniqueID = (self.item_manufacturer +
self.item_product_num +
self.item_lot +
datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
super(InventoryItem, self).save(*args, **kwargs)
可能尝试以下操作:
class InventoryItem(models.Model):
item_name = models.TextField(max_length = 100)
item_manufacturer = models.TextField(max_length = 20)
item_product_num = models.TextField(max_length = 25)
item_lot = models.TextField(max_length = 25)
item_received_on = models.DateTimeField(auto_now_add = True)
item_price = models.DecimalField()
item_quantity = models.DecimalField()
item_special_instructions = models.TextField(default = "NA", max_length = 200)
item_reinspection_date = models.DateField()
item_uniqueID = models.TextField(max_length = 50)
def save(self, *args, **kwargs):
if not self.item_uniqueID:
self.item_uniqueID = (self.item_manufacturer +
self.item_product_num +
self.item_lot +
datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
super(InventoryItem, self).save(*args, **kwargs)
我将通过覆盖模型上的save函数来实现这一点
class InventoryItem(models.Model):
#Other Fields
item_uniqueID = models.TextField(max_length = 50)
def save(self, *args, **kwargs):
self.item_uniqueID = self.item_manufacturer + self.item_product_num + self.item_lot + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
super(InventoryItem, self).save(*args, **kwargs)
我将通过覆盖模型上的save函数来实现这一点
class InventoryItem(models.Model):
#Other Fields
item_uniqueID = models.TextField(max_length = 50)
def save(self, *args, **kwargs):
self.item_uniqueID = self.item_manufacturer + self.item_product_num + self.item_lot + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
super(InventoryItem, self).save(*args, **kwargs)