Ich mache eine E -Commerce -Site, ich möchte Cartelemente in einem Integer -Array -Feld speichern. Ich verwende PostgreSQL als meine Datenbank. Hier sind meine Modelle < /p>
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])
< /code>
Unten ist mein Formular.py. Ich habe nur grundlegende Form
aus Django -Importformularen
aus .models importieren usercart
aus django.db import Models
aus django.contrib.postgres.fields importieren arrayfield < /p>
class UserCartForm (forms.ModelForm):
class Meta:
model= UserCart
fields = ('user_product',)
< /code>
Ich habe im Internet viel gesucht, aber die relevante Antwort nicht finden. Ich möchte, dass bei der Schaltfläche "Zu CART add to car add add to cart klickt", wird prot product_id in cart_products array gelesen.@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)
< /code>
Es gibt mir Fehler, dass < /p>
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
< /code>
Und wenn ich auf diese Weise carts speichere < /p>
post.cart_products=99
< /code>
Dann wirft er diesen Fehler < /p>
aus 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.
< /code>
Bitte helfen Sie mir in dieser Angelegenheit.>
Wie man sich an das Array -Feld in Python Django angehängt hat ⇐ Python
-
- Similar Topics
- Replies
- Views
- Last post