im new to django tryna build up ecommerce project for education purposes. i got this error " str returned non-string (type NoneType)" from admin panel, when try view/change order or orderItem. I try to return str(self)I cant find error, pls guys help me fix it. I almost google all, but i dont understand how to can be type error if i return str(). Pls guys help my find they way to fix it
from django.core.validators import RegexValidator
from django.db import models
from authentication.models import Customer
from catalog.models import Product
# Create your models here.
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True)
date_order = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False, null=True, blank=True)
transaction_id = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return str(self.transaction_id)
@property
def shipping(self):
shipping = False
orderitems = self.orderitem_set.all()
for i in orderitems:
if i.product.digital == False:
shipping = True
return shipping
@property
def get_cart_total(self):
orderitems = self.orderitem_set.all()
total = sum([item.get_total for item in orderitems])
return total
@property
def get_cart_items(self):
orderitems = self.orderitem_set.all()
total = sum([item.quantity for item in orderitems])
return total
class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
@property
def get_total(self):
total = self.product.price * self.quantity
return total
class ShippingAddress(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True,
blank=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True,
blank=True)
phone_regex = RegexValidator(
regex=r'^(05)\d{9}$'
)
mobile = models.CharField(validators=[phone_regex], max_length=60,
null=True, blank=True)
state = models.CharField('Область', max_length=200, null=True)
city = models.CharField('Город', max_length=200, null=True)
address = models.CharField('Адрес', max_length=200, null=True, blank=True)
zipcode = models.CharField('Почтовый индекс', max_length=200, null=True)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.address
class Wishlist(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True,
blank=True)
date_order = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False, null=True, blank=True)
transaction_id = models.CharField(max_length=200, null=True)
def __str__(self):
return str(self.id)
class WishlistItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
wishlist = models.ForeignKey(Wishlist, on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.id)
Here is my views:
from django.shortcuts import render
from .models import *
from django.http import JsonResponse
import json
import datetime
from utils import cookieCart, cartData, guestOrder
from django.conf import settings
from django.core.mail import send_mail
# Create your views here.
def cart(request):
data = cartData(request)
cartItems = data['cartItems']
order = data['order']
items = data['items']
context = {'order': order, 'cartItems': cartItems, 'items': items}
return render(request, 'main/cart.html', context)
def checkout(request):
data = cartData(request)
cartItems = data['cartItems']
order = data['order']
items = data['items']
context = {'items': items, 'order': order, 'cartItems': cartItems}
return render(request, 'main/checkout.html', context)
def wishlist(request):
data = cartData(request)
cartItems = data['cartItems']
customer = request.user.customer
wishlist, created = Wishlist.objects.get_or_create(customer=customer,
complete=False)
items = wishlist.wishlistitem_set.all()
context = {
'items': items,
'cartItems': cartItems,
}
return render(request, 'main/wishlist.html', context)
def updateItem(request):
data = json.loads(request.body)
productId = data['productId']
action = data['action']
print('Action:', action)
print('Product:', productId)
customer = request.user.customer
product = Product.objects.get(id=productId)
order, created = Order.objects.get_or_create(customer=customer, complete=False)
orderItem, created = OrderItem.objects.get_or_create(order=order,
product=product)
if action == 'add':
orderItem.quantity = (orderItem.quantity + 1)
elif action == 'remove':
orderItem.quantity = (orderItem.quantity - 1)
orderItem.save()
if orderItem.quantity <= 0:
orderItem.delete()
return JsonResponse('Item was added', safe=False)
def processOrder(request):
transaction_id = datetime.datetime.now().timestamp()
data = json.loads(request.body)
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer,
complete=False)
else:
customer, order = guestOrder(request, data)
total = data['form']['total']
order.transaction_id = transaction_id
if total == order.get_cart_total:
order.complete = True
order.save()
if order.shipping:
ShippingAddress.objects.create(
customer=customer,
order=order,
mobile=data['shipping']['mobile'],
state=data['shipping']['state'],
city=data['shipping']['city'],
address=data['shipping']['address'],
zipcode=data['shipping']['zipcode'],
)
print('Data:', request.body)
return JsonResponse('Payment submitted..', safe=False)
I think culprit is your ShippingAddress __str__ method. It returns address
which can be None
since it has flag null=True
def __str__(self):
return self.address # <= can return None
Replace it with f-string (I recommend replacing all str())
def __str__(self):
return f"{self.address}"
or give some default_name
def __str__(self):
return self.address or "Default no name string"
Solved problem, the error was in Customer model which is relate to Order model and other. Just replace def str with Bartosz Stasiak advice
def __str__(self):
return f"{self.address}"
It helps to me, thank you so much!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.