Здесь я пытаюсь сохранить Seller
в модели Order
после того, как Customer
разместит успешный заказ. Поскольку вошедший в систему User
является Customer
, поэтому он показывает RelatedObjectDoesNotExist: User has no seller.
Но я хочу сохранить Seller
после того, как Customer
разместит заказ, чтобы Seller
можно было уведомить, что Seller
есть новый заказ. Но в Order Model
, где seller = models.ForeignKey(Seller, blank=True, null=True, on_delete=models.CASCADE)
, здесь это не спасает продавца.
'cart views.py`
def checkout(request):
cart_obj, cart_created = Cart.objects.new_or_get(request)
order_obj = None
if cart_created or cart_obj.products.count() == 0:
return redirect('cart:cart')
login_form = CustomerLoginForm()
signin_form = CreateCustomerForm()
address_form = AddressForm()
billing_address_id = request.session.get("billing_address_id", None)
shipping_address_id = request.session.get("shipping_address_id", None)
billing_profile, billing_profile_created = BillingProfile.objects.new_or_get(request)
address_qs = None
customer = request.user.customer
seller = request.user.seller
if billing_profile is not None:
if request.user.is_authenticated:
address_qs = Address.objects.filter(billing_profile=billing_profile)
order_obj, order_obj_created = Order.objects.new_or_get(billing_profile, cart_obj, customer, seller)
if shipping_address_id:
order_obj.shipping_address = Address.objects.get(id=shipping_address_id)
del request.session["shipping_address_id"]
if billing_address_id:
order_obj.billing_address = Address.objects.get(id=billing_address_id)
del request.session["billing_address_id"]
if billing_address_id or shipping_address_id:
order_obj.save()
if request.method == "POST":
is_done = order_obj.check_done()
if is_done:
order_obj.mark_paid()
request.session['cart_items'] = ""
del request.session['cart_id']
return redirect("cart:success")
context = {
'object':order_obj,
'billing_profile':billing_profile,
'login_form':login_form,
'signin_form': signin_form,
'address_form':address_form,
'address_qs': address_qs,
}
return render(request, 'cart/checkout.html', context)
accounts models.py
class UserManager(BaseUserManager):
"""Define a model manager for User model with no username field."""
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
class User(AbstractUser):
"""User model."""
username = None
email = models.EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager() ## This is the new line in the User model. ##
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
email = models.EmailField(_('email address'), unique=True)
full_name = models.CharField(max_length=120, null=True, blank=True)
phone = models.IntegerField(null=True, blank=True)
profile_pic = models.ImageField(upload_to='profile-image/', default='profile1.png', null=True, blank=True)
def __str__(self):
return self.user.email
class Seller(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
email = models.EmailField(_('email address'), unique=True)
full_name = models.CharField(max_length=120, null=True, blank=True)
phone = models.IntegerField(null=True, blank=True)
profile_pic = models.ImageField(upload_to='profile-image/', default='profile1.png', null=True, blank=True)
def __str__(self):
return self.user.email
cart models.py
class CartManager(models.Manager):
# creating a new cart or getting the current one
def new_or_get(self, request):
cart_id = request.session.get("cart_id", None)
qs = self.get_queryset().filter(id=cart_id)
if qs.count() == 1:
new_obj = False # the cart exists
cart_obj = qs.first()
if request.user.is_authenticated and cart_obj.user is None:
cart_obj.user = request.user
cart_obj.save()
else:
cart_obj = Cart.objects.new(user=request.user)
new_obj = True # Ceated new
request.session['cart_id'] = cart_obj.id
return cart_obj, new_obj
# Associating the user to the cart
def new(self, user=None):
user_obj = None
if user is not None:
if user.is_authenticated:
user_obj = user
return self.model.objects.create(user=user_obj)
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
products = models.ManyToManyField(Product, blank=True)
quantity = models.IntegerField(default=1)
subtotal = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
total = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
objects = CartManager()
def __str__(self):
return str(self.id)
product models.py
class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(max_length=400)
categories = models.CharField(max_length=120, choices=CATEGORIES)
volume = models.DecimalField(decimal_places=2,max_digits=10, default=0.00)
volume_type = models.CharField(max_length=120, choices=PRODUCT_VOL_TYPE)
product_stock = models.IntegerField(default=0.00)
price = models.DecimalField(decimal_places=2,max_digits=10, default=0.00)
image = models.ImageField(upload_to='products/', null=True, blank=True)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(blank=True, unique=True)
seller = models.ForeignKey(Seller, on_delete=models.CASCADE)
objects = ProductManager()
def __str__(self):
return self.title
order models.py
class OrderManager(models.Manager):
def new_or_get(self, billing_profile, cart_obj, customer, seller):
created = False
qs = self.get_queryset().filter(cart=cart_obj, billing_profile=billing_profile, customer=customer, seller=seller, active=True, status='created')
if qs.count() == 1:
obj = qs.first()
else:
obj = self.model.objects.create(billing_profile=billing_profile, customer=customer, seller=seller, cart=cart_obj)
created = True
return obj, created
class Order(models.Model):
order_id = models.CharField(max_length=120, blank=True)
billing_profile = models.ForeignKey(BillingProfile, blank=True, null=True, on_delete=models.CASCADE)
shipping_address = models.ForeignKey(Address, related_name="shipping_address", null=True, blank=True, on_delete=models.CASCADE)
billing_address = models.ForeignKey(Address, related_name="billing_address", null=True, blank=True, on_delete=models.CASCADE)
shipping_total = models.DecimalField(default=5.99, max_digits=100, decimal_places=2)
total = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
status = models.CharField(max_length=120, default='created', choices=ORDER_STATUS_CHOICES)
active = models.BooleanField(default=True)
cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
customer = models.ForeignKey(Customer,blank=True, null=True, on_delete=models.CASCADE)
seller = models.ForeignKey(Seller, blank=True, null=True, on_delete=models.CASCADE)
objects = OrderManager()
def __str__(self):
return self.order_id
Traceback
Внутренний сервер Ошибка: / cart / checkout /
Трассировка (последний последний вызов):
Файл "D: \ mondayBS \ lib \ site-packages \ django \ core \ handlers \ exception.py" , строка 34, во внутреннем ответе = get_response (запрос)
Файл "D: \ mondayBS \ lib \ site-packages \ django \ core \ handlers \ base.py", строка 115, в _get_response response = self .process_exception_by_middleware (e, запрос)
Файл "D: \ mondayBS \ lib \ site-packages \ django \ core \ handlers \ base.py", строка 113, в _get_response response = wrapped_callback (request, * callback_args) , ** callback_kwargs)
Файл "D: \ mondayBS \ src \ cart \ views.py", строка 68, в кассе продавца = request.user.seller
Файл "D: \ mondayBS \ lib \ site-packages \ django \ utils \ functions.py ", строка 225, во внутренней рету rn fun c (self._wrapped, * args)
Файл "D: \ mondayBS \ lib \ site-packages \ django \ db \ models \ fields \ related_descriptors.py", строка 420, в получить
поднять себя.
order_obj, order_obj_created = Order.objects.new_or_get(billing_profile, cart_obj, customer, seller)
order models.py
def new_or_get(self, billing_profile, cart_obj, customer, seller):
created = False
qs = self.get_queryset().filter(cart=cart_obj, billing_profile=billing_profile, customer=customer, seller=seller, active=True, status='created')
if qs.count() == 1:
obj = qs.first()
else:
obj = self.model.objects.create(billing_profile=billing_profile, customer=customer, seller=seller, cart=cart_obj)
created = True
return obj, created