简体   繁体   English

AttributeError: 'Price' 对象没有属性 'update'

[英]AttributeError: 'Price' object has no attribute 'update'

I got an error,AttributeError: 'Price' object has no attribute 'update' .I wrote我收到一个错误,AttributeError: 'Price' object has no attribute 'update' .I 写道

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在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)

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?我想把“○”放在价格模型的upper1000&from500to1000&under500列,但由于错误而无法完成。我的代码有什么问题?我该如何解决这个问题?

.update is a method on querysets, not models. .update是查询集上的一种方法,而不是模型。 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: django 模型实例has no attribute 'update'update是 django objects Manager 类的方法,如果你想更新单个实例,你可以尝试:

Price.objects.filter(pk=price.pk).update(
    upper1000="○",
    from500to1000="○",
    under500="○"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“元组”对象没有属性“价格” - AttributeError: 'tuple' object has no attribute 'price' AttributeError:…对象没有属性'update' - AttributeError: … object has no attribute 'update' Python AttributeError:“ str”对象没有属性“ get_price” - Python AttributeError: 'str' object has no attribute 'get_price' AttributeError:类型对象“TestPages”没有属性“test_sort_by_price” - AttributeError: type object 'TestPages' has no attribute 'test_sort_by_price' AttributeError-类型对象“服务”没有属性“ service_price” - AttributeError - type object 'Services' has no attribute 'service_price' AttributeError:“熊猫”对象没有属性“描述单价” - AttributeError: 'Pandas' object has no attribute 'Description Unit Price' Python,AttributeError:“ PlayerPaddle”对象没有属性“ update” - Python, AttributeError: 'PlayerPaddle' object has no attribute 'update' AttributeError:“列表”对象没有属性“更新” - AttributeError: 'list' object has no attribute 'update' AttributeError:'NoneType'对象没有属性'update' - AttributeError: 'NoneType' object has no attribute 'update' AttributeError 'DictCursor' object 没有属性 'update' - AttributeError 'DictCursor' object has no attribute 'update'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM