Python 属性错误:';长';对象没有属性';保存';
我在运行django应用程序时出错。 这是models.py,最后一个字段是cart_count,在添加该字段后,我在其中出错。 models.pyPython 属性错误:';长';对象没有属性';保存';,python,django,Python,Django,我在运行django应用程序时出错。 这是models.py,最后一个字段是cart_count,在添加该字段后,我在其中出错。 models.py class Series(models.Model): name = models.CharField(max_length=200) tutor = models.ForeignKey(Tutor) category = models.ForeignKey(Category) description = model
class Series(models.Model):
name = models.CharField(max_length=200)
tutor = models.ForeignKey(Tutor)
category = models.ForeignKey(Category)
description = models.CharField(max_length=10000)
video = models.CharField(max_length=200)
image = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2)
GBP = models.DecimalField(max_digits=10, decimal_places=2)
USD = models.DecimalField(max_digits=10, decimal_places=2)
Euro = models.DecimalField(max_digits=10, decimal_places=2)
showcase = models.BooleanField(default=True)
genre = models.CharField(max_length=50)
difficulty = models.CharField(("Difficulty"), max_length=10, choices=DIFFICULTY)
slug = models.SlugField(max_length=200, unique=True)
is_active = models.BooleanField(default=True)
seo_meta = models.CharField(max_length=2000)
seo_meta_keywords = models.ManyToManyField(Keyword)
video_filename = models.CharField(max_length=100, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
related_series = models.ManyToManyField("self")
is_backing_tracks = models.BooleanField(default=False)
enable_lesson_previews = models.BooleanField(default=False)
cart_count = models.IntegerField(default=0)
my views.py代码
views.py
输出误差
Exception Type: AttributeError
Exception Value:'long' object has no attribute 'save'
您正在长对象上调用save()方法,该方法应在queryset上调用,因为save()方法未在长对象上定义您将遇到此错误,save()方法是Django ORM的一部分,用于保存新条目或更新现有条目,因此,应该是这样的
简单地替换
count=series.cart\u count
计数+=1
count.save()
与
series.cart\u计数+=1
series.save()
在您的代码中,计数只是一个整数,实际上您应该保存对象。这里的对象是序列
def add_to_cart_series(request, sid):
# TODO have to redirect to cart page if the series already bought.
series = Series.objects.get(id=sid)
series.cart_count += 1
series.save()
问题是count是一个整数变量,您没有用于它的方法
save()
。如果要将对象的属性series.cart\u count
增加1,只需这样做
series.cart_count += 1
series.save()
不需要变量计数(假设这是您的预期行为)。是。你为什么要在计数时调用
save()
?
series.cart_count += 1
series.save()