Hi,
-- I've been facing a problem recently in creating a Django Profile model for an E-commerce website.
If a profile updates it's cart then all the carts in the admin page are getting updated with the same items.
Kindly help me out if possible. The screenshots have been attached below and the github link for the project has been provided.
Regards,
Adarsh
models.py:
class Profile(BaseModel):
user = models.OneToOneField(User, on_delete=models.CASCADE)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User) #add this
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User) #add this
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
class Cart(BaseModel):
user = models.ForeignKey(User,
on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
being_delivered = models.BooleanField(default=False)
received = models.BooleanField(default=False)
refund_requested = models.BooleanField(default=False)
refund_granted = models.BooleanField(default=False)
def __str__(self):
return self.user.username
def get_total_items(self):
total = 0
for item in self.items.all():
total = sum(item.quantity)
return total
def get_total(self):
total = 0
for order_item in self.items.all():
total += order_item.get_final_price()
return totalviews.py:@login_required
def add_to_cart(request, slug):
item = get_object_or_404(Items, slug=slug)
order_item, created = OrderItem.objects.get_or_create(
product=item,
user=request.user,
ordered=False
)
order_qs = Cart.objects.filter(user=request.user, ordered=False)
if order_qs.exists():
order = order_qs[0]
if order.items.filter(product__slug=item.slug).exists():
order_item.quantity += 1
order_item.save()
messages.info(request, "This item quantity was updated.")
return redirect(reverse('store:cart-page'))
else:
order.items.add(order_item)
messages.info(request, "This item was added to your cart.")
return redirect(reverse('store:cart-page'))
else:
ordered_date = timezone.now()
order = Cart.objects.create(user=request.user, ordered_date=ordered_date)
order.items.add(order_item)
messages.info(request, "This item was added to your cart.")
return redirect(reverse('store:cart-page'))def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Account created for {username}!')
return redirect(reverse('store:home-page'))
else:
form = UserRegisterForm()
return render(request, 'register.html', {'form': form})
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANNRaDATMp0LjSrrRv80f9FbDF1jXpKFGCZbg4v048G%3DFGgoNg%40mail.gmail.com.
No comments:
Post a Comment