Reputation: 143
I got an error,AttributeError: 'Price' object has no attribute 'update' .I wrote
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="○")
In models.py I wrote
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)
I wanna put "○" to upper1000&from500to1000&under500 column of Price model, but it cannot be done because of the error.What is wrong in my code?How can I fix this?
Upvotes: 4
Views: 9146
Reputation:
django model instance has no attribute 'update'
, the update
is the method of django objects Manager class if you want to update single instance you can try:
Price.objects.filter(pk=price.pk).update(
upper1000="○",
from500to1000="○",
under500="○"
)
Upvotes: 9
Reputation: 599788
.update
is a method on querysets, not models. It's useful if you want to update a bunch of records that share some query criteria.
The normal way of updating an object you already have is to set its attributes and then save it.
price.upper1000 = "○"
price.from500to1000 = "○"
price.under500 = "○"
price.save()
Upvotes: 12