我启动了车队应用程序,并在模型相关文件中创建了一些模型,我进行了模型层次结构,上面是我拥有汽车品牌,下面是与每个品牌相关的ForeignKey相关汽车模型,以及每个最终构成ID的汽车ID的ForeignKey汽车库存,称为“ CarPool类”,在此模型中,我的值是“ car_sale_status = models.BooleanField(default = False)”,我想实现的是每次我根据卖方的关联ForeignKey创建一个实例时自动将“ car_sale_status = models.BooleanField(default = False)”更新为True
请咨询
谢谢
class CarPool(models.Model):
# Relationship Fields
car_inventory = models.ForeignKey(
'cardealer.CarTypes',
on_delete=models.CASCADE, related_name="carpools",
)
car_id = models.CharField(max_length=30,default="")
car_location = GeopositionField()
car_sale_status = models.BooleanField(default=False)
YEAR_CHOICES = []
for r in range(1980, (datetime.datetime.now().year+1)):
YEAR_CHOICES.append((r,r))
year_manufacture = models.IntegerField(('year_manufacture'), choices=YEAR_CHOICES, default=datetime.datetime.now().year)
def __str__(self):
return self.car_id
def __unicode__(self):
return self.car_inventory
class CustomerBuyer(models.Model):
buyer_car_buy = models.ForeignKey(
'cardealer.CarPool',
on_delete=models.CASCADE, related_name="customer_buyer",default='0'
)
buyer_private_name = models.CharField(max_length=80,default='')
buyer_family_name = models.CharField(max_length=80,default='')
buyer_private_id = models.IntegerField(default=0)
transaction_date = models.DateTimeField(default=datetime.datetime.now())
transaction_amount = models.IntegerField(default=0)
def __str__(self):
return self.buyer_private_name + ' '+ self.buyer_family_name
def __unicode__(self):
return self.buyer_private_name + ' '+ self.buyer_family_name
答案 0 :(得分:1)
如果我对您的理解正确,只需扩展“保存”方法
class CustomerBuyer(models.Model):
buyer_car_buy = models.ForeignKey(
'cardealer.CarPool',
on_delete=models.CASCADE, related_name="customer_buyer",default='0'
)
buyer_private_name = models.CharField(max_length=80,default='')
buyer_family_name = models.CharField(max_length=80,default='')
buyer_private_id = models.IntegerField(default=0)
transaction_date = models.DateTimeField(default=datetime.datetime.now())
transaction_amount = models.IntegerField(default=0)
def __str__(self):
return self.buyer_private_name + ' '+ self.buyer_family_name
def __unicode__(self):
return self.buyer_private_name + ' '+ self.buyer_family_name
def save(self, *arg, **kwargs):
super(CustomerBuyer, self).save(*args, **kwargs)
self.buyer_car_buy.car_sale_status = True
self.buyer_car_buy.save()