我收到错误,AttributeError:'Price'对象没有属性'update'。我写了
fourrows_transpose=list(map(list, zip(*fourrows)))
val3 = sheet3.cell_value(rowx=0, colx=9)
user3 = Companyransaction.objects.filter(corporation_id=val3).first()
if user3:
area = Area.objects.filter(name="America").first()
pref = Prefecture.objects.create(name="Prefecture", area=area)
city = City.objects.create(name="City", prefecture=pref)
price= Price.objects.create(city=city)
pref.name = fourrows_transpose[0][0]
pref.save()
for transpose in fourrows_transpose[2:]:
if len(transpose) == 5:
if "×" in transpose or "○" in transpose:
city.name = "NY"
city.save()
price.update(upper1000="○",from500to1000="○",under500="○")
在models.py中,我写了
class Area(models.Model):
name = models.CharField(max_length=20, verbose_name='area', null=True)
class User(models.Model):
user_id = models.CharField(max_length=200,null=True)
area = models.ForeignKey('Area',null=True, blank=True)
class Prefecture(models.Model):
name = models.CharField(max_length=20, verbose_name='prefecture')
area = models.ForeignKey('Area', null=True, blank=True)
class City(models.Model):
name = models.CharField(max_length=20, verbose_name='city')
prefecture = models.ForeignKey('Prefecture', null=True, blank=True)
class Price(models.Model):
upper1000 = models.CharField(max_length=20, verbose_name='u1000', null=True)
from500to1000 = models.CharField(max_length=20, verbose_name='500~1000', null=True)
under500 = models.CharField(max_length=20, verbose_name='d500', null=True)
city = models.ForeignKey('City', null=True, blank=True)
我想把“○”放在售价1000以上的500到1000以及500以下的价格模型列中,但由于错误而无法完成。我的代码出了什么问题?如何解决这个问题?
答案 0 :(得分:5)
.update
是查询集的方法,而不是模型。如果您想要更新一组共享某些查询条件的记录,这非常有用。
更新已有对象的常规方法是设置其属性,然后保存它。
price.upper1000 = "○"
price.from500to1000 = "○"
price.under500 = "○"
price.save()
答案 1 :(得分:1)
django模型实例has no attribute 'update'
,update
是django对象Manager类的方法,如果你想更新单个实例,你可以试试:
Price.objects.filter(pk=price.pk).update(upper1000="○",from500to1000="○",under500="○")