AttributeError:' long'对象没有属性' save'

时间:2015-10-16 06:33:02

标签: python django

运行我的django应用程序时出现此错误。 这是models.py,最后一个字段是cart_count,我添加该字段后出错。 models.py

myApp.directive("speakerCards", function() {
    return {
        restrict: "E",
        templateUrl: "template.html",
      scope: {
            speakers: "=data"
        }
    };
});

我的views.py代码 views.py

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)

输出错误

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)
    count=series.cart_count
    count+=1
    count.save()
    print count

3 个答案:

答案 0 :(得分:1)

问题是count是一个整数变量,你没有方法save()。如果您想将对象series.cart_count的属性series加1,您只需

series.cart_count += 1
series.save()

不需要变量计数(假设这是您的预期行为)。

答案 1 :(得分:0)

你在long对象上调用save()方法应该在queryset上调用,因为没有在long对象上定义save()方法你得到这个错误,save()方法是Django ORM的一部分,它是使用的保存新条目或更新现有条目,所以,这应该是这样的,

简单地替换

count=series.cart_count count+=1 count.save()

series.cart_count += 1 series.save()

答案 2 :(得分:0)

在您的代码计数中只是一个集成器,实际上您应该保存该对象。这里的对象是系列。

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