2013-05-27 36 views
1

我一直在试图在我的网站上实现一个基于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

在此先感谢。

回答

1
  1. 我认为你应该使用UserShoppingCart之间OneToOne关系。

样品型号:

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,如果它不存在,它

由于

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 
+0

我摆脱了没有的输入错误。谢谢! 但现在最新情况是,每次尝试将歌曲添加到购物车时,都会创建多个购物车。我打开数据库,发现监听器表中没有记录cart_id。这意味着听众和购物车之间没有关系。 这个错误可能是由.save()的错误实现引起的吗? – Karthik

+0

@karthik,检查更新的代码,创建'cart'对象并保存,然后分配并保存到'listener'中。 – Rohan

+0

谢谢!对于进一步的编辑,request.usr.listener.save()不起作用,request.usr.listener.cart.save()重现相同的错误。 – Karthik

1

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

还有就是你Listener模型和User模型(即使你从它继承。)之间没有关系,所以无论你正在尝试做的,它不会工作。事实上,即使存在关系,您也会看到这些错误,因为您没有正确使用对象关系。

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

如果您只想跟踪购物车的会话;然后使用sessions来做到这一点。

由于您没有正确使用add(),它可能会使您通过文档的relations section受益。

相关问题