无类型错误属性

时间:2013-05-27 11:02:49

标签: django django-models django-views

我一直在尝试在我的网站上实现基于Django的购物车。

以下是我使用的模型:

class ShoppingCart(models.Model):
    songs = models.ManyToManyField(Song)
    albums = models.ManyToManyField(Album)

class Listener(User):
    name = models.CharField(max_length=200, blank=True, null=True)
    bio = models.TextField(blank=True, null=True)
    cart = models.ForeignKey(ShoppingCart, blank=True, null=True)

以下views.py我收到错误None Type Object has no attribute songs at request.user.listener.cart.songs.add()

def add_to_cart(request):
    if not request.user.is_authenticated() or not request.method == 'POST':
        return HttpResponse(json.dumps({'success':False}), content_type='application/json')
    typeobj = request.POST.get('type', None)
    obj = request.POST.get('id', None)
    if typeobj is None or obj is None:
        return HttpResponse(json.dumps({'success':False}), content_type='application/json')

    if typeobj == 'album':
        try:
            album = Album.objects.get(pk=obj)
        except ObjectDoesNotExist:
            return HttpResponse(json.dumps({'success':False}), content_type='application/json')
        request.user.listener.cart.albums.add(Album.objects.get(pk=obj))
    else:
        try:
            song = Song.objects.get(pk=obj)
        except ObjectDoesNotExist:
            return HttpResponse(json.dumps({'success':False}), content_type='application/json')
        request.user.listener.cart.songs.add(Song.objects.get(pk=obj))
    return HttpResponse(json.dumps({'success':True}), content_type='application/json')

我检查了shell,当我尝试将歌曲添加到购物车时发生了同样的错误。它表示购物车是NoneType对象,没有属性songs

提前致谢。

2 个答案:

答案 0 :(得分:1)

request.user是当前登录用户的User对象的实例。

您的Listener模型与User模型之间没有任何关系(即使您继承了它),因此无论您尝试做什么,它都无法正常工作。事实上,即使存在关系,您也会看到这些错误,因为您没有正确使用对象关系。

如果您想要永久跟踪每个用户的购物车,您需要在ForeignKey模型中添加ShoppingCart以指向User模型。

如果您只想跟踪会话的购物车;然后使用sessions来执行此操作。

通过文档的relations section可能会对您有所帮助,因为您没有正确使用add()

答案 1 :(得分:1)

  1. 我认为您应该在OneToOneUser之间使用ShoppingCart关系。
  2. 样本模型:

    class Listener(User):
       name = models.CharField(max_length=200, blank=True, null=True)
       bio = models.TextField(blank=True, null=True)
       cart = models.OneToOneField(ShoppingCart, blank=True, null=True)
    
    1. 在您的视图中,为用户创建cart(如果不存在)
    2. 作为

      def add_to_cart(request):
          if not request.user.is_authenticated() or not request.method == 'POST':
             return HttpResponse(json.dumps({'success':False}), content_type='application/json')
          typeobj = request.POST.get('type', None)
          obj = request.POST.get('id', None)
          if typeobj is None or obj is None:
              return HttpResponse(json.dumps({'success':False}), content_type='application/json')
      
          if request.usr.listener.cart == None:
               cart = ShoppingCart()
               cart.save()
               request.usr.listener.cart = cart
               request.usr.listener.save()
      
          # your code to add items in cart