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?
If you are getting an object that has no attribute error then the reason behind it is because your indentation is goofed, and you've mixed tabs and spaces.
In the example above, object b has the attribute disp , so the hasattr() function returns True. The list doesn't have an attribute size , so it returns False. If we want an attribute to return a default value, we can use the setattr() function. This function is used to create any missing attribute with the given value.
The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.
.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()
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="○"
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With