How to Append to Array Field in Python Django
23:50 25 Aug 2017

I am making an E commerce site, I want to store Cart elements in an integer Array Field. I am using PostGreSql as my database. I have created model for cart by extending Django User model. Here is my models

class UserCart(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE)
    user_product=models.IntegerField(blank=True, null=True)
    cart_products = ArrayField(
        models.IntegerField(blank=True),
        default = list
    )

User.profile = property(lambda u:UserCart.objects.get_or_create(user=u)[0])

Below is my Form.py. I have created only basic form from django import forms from .models import UserCart from django.db import models from django.contrib.postgres.fields import ArrayField

class UserCartForm (forms.ModelForm):

    class Meta:
        model= UserCart
        fields = ('user_product',)

I have searched alot on internet but was unable to find the relevant answer.I want that whenever user clicks on Add to Cart button, that product_id gets stored in cart_products array.I read somewhere that ArrayFields behave as list in Django, so here is my views.py

@login_required
def user_cart(request):
    if request.method=='POST':
        form=UserCartForm(request.POST , instance=request.user.profile)
        if form.is_valid():
            post = form.save(commit=False)
            post.cart_products.append(99)
            post.save()
            return HttpResponseRedirect('/user_login/loggedin')
        else:
            HttpResponse("Error")
    else:
        user=request.user
        profile=user.profile
        form= UserCartForm(instance=profile)
        args={}
        args.update(csrf(request))
        args['form']=form
        return render_to_response('cartapi.html' ,args)

Its giving me Error that

 AttributeError at /cart/ac/
 'NoneType' object has no attribute 'append'
 Request Method:    POST
 Request URL:   http://localhost:8000/cart/ac/
 Django Version:    1.11.2
 Exception Type:    AttributeError
 Exception Value:   
 'NoneType' object has no attribute 'append'
 Exception Location:    C:\Users\Muhammad                
 Jawad\AppData\Local\Programs\Python\Python36-32\mysite\cart\views.py in 
 user_cart, line 19
 Python Executable: C:\Users\Muhammad 
 Jawad\AppData\Local\Programs\Python\Python36-32\python.exe

And if i save cart_products this way

 post.cart_products=99

Then it throws this error

 column "cart_products" is of type int4range but expression is of type integer
 LINE 1: ...er_id" = 1, "user_cart" = 3000, "cart_products" = 99 WHERE "...
                                                         ^
 HINT:  You will need to rewrite or cast the expression.
 Request Method:    POST
 Request URL:   http://localhost:8000/cart/ac/
 Django Version:    1.11.2
 Exception Type:    ProgrammingError
 Exception Value:   
 column "cart_products" is of type int4range but expression is of type integer
 LINE 1: ...er_id" = 1, "user_cart" = 3000, "cart_products" = 99 WHERE "...
                                                         ^
 HINT:  You will need to rewrite or cast the expression.

Kindly Help me in this matter.Summarizing my Question: How can i get user_product as id and save it in cart_products

python arrays django windows postgresql