我试图获取值的模型的主键与我试图从中获取列表的列的主键不同。
编辑:添加更多关于我想做什么的详细信息:我想从CustomerCatalog获得一个列表,其中所有记录在其“ccname”字段中的值与服务器模型/表单在其“account”字段中的值相同。这样,当我为特定帐户添加服务器时,它将在CustomerCatalog中查找针对该特定帐户的所有产品。
这是我的想法,但它不起作用:
class Server(models.Model):
"""
Model representing list of servers per contract
"""
os_license = models.CharField(max_length=95, blank=True, choices=product_list)
account = models.ForeignKey('Account', on_delete=models.SET_NULL, null=True)
@staticmethod
def product_list():
return CustomerCatalog.objects.filter(account=Server.account).values_list('ccname', flat=True)
以下是CustomerCatalog模型:
class CustomerCatalog(models.Model):
"""
Model for representing products that have been sold and are being used by a specific customer.
"""
id = models.AutoField(primary_key=True)
ccproductid = models.ForeignKey('ProductCatalog', on_delete=models.SET_NULL, null=True, verbose_name="Product ID")
ccname = models.CharField(max_length=200, verbose_name="Product name", blank=True)
account = models.ForeignKey('Account', on_delete=models.SET_NULL, null=True, blank=True, help_text='Account to which this product was sold.')
unit = models.CharField(max_length=50, help_text='Unit of measurement.', null=True, blank=True, default='VM')
unit_price = models.DecimalField(max_digits=30, decimal_places=2, help_text="Enter price per unit.", null=True, blank=True)
total_qty = models.DecimalField(max_digits=30, decimal_places=2, help_text="Enter quantity.", null=True, blank=True)
total_price = models.DecimalField(max_digits=30, decimal_places=2, help_text="Enter total price (note this will be calculated in a future release).", null=True, blank=True)
in_contract = models.BooleanField(null=False, blank=False, default=True)
history = HistoricalRecords()
@property
def get_ccname(self):
return self.ccproductid.name
def save(self, *args, **kwargs):
self.ccname = self.get_ccname
super(CustomerCatalog, self).save(*args, **kwargs)
class Meta:
verbose_name_plural = 'Customer Catalog'
def __str__(self):
"""
String for representing the Model object (in Admin site etc.)
"""
return f'{self.ccname}'
这是产品目录。这是所有产品的唯一列表,以及将产品分配给单个客户时索引的内容。
class ProductCatalog(models.Model):
"""
Model to represent the full product portfolio.
"""
id = models.CharField(max_length=40, help_text="Enter Product ID.", primary_key=True)
name = models.CharField(max_length=255, help_text="Enter name of resource unit that will be used with service definitions.", unique=True)
billing = models.CharField(max_length=70, help_text="Enter billing type.")
unit = models.CharField(max_length=80, help_text='Unit of measurement.')
short_description = models.TextField(max_length=400, help_text="Enter the description of the service.", blank=True)
version = models.DateField(default=timezone.now, help_text="Enter date of the CPS version.")
servicecat = models.ForeignKey('Service', on_delete=models.SET_NULL, null=True, blank=True, help_text='Enter the service in which this product belongs.')
org = models.ForeignKey(OrgUnit, on_delete=models.SET_NULL, null=True, help_text="Enter organization providing the service.")
history = HistoricalRecords()
class Meta:
verbose_name_plural = 'Product Catalog'
def __str__(self):
"""
String for representing the Model object (in Admin site etc.)
"""
return f'{self.id}'