Y
Y
Yura Khlyan2015-12-14 19:26:59
Django
Yura Khlyan, 2015-12-14 19:26:59

How to create a new cart?

Good day.
Background:
There was a task: to make 5 stores with one back office, but there with different prices, etc. After many discussions and trials, we decided to use one database and connect it django.contrib.sitesto separate content across sites. For stores, we chose Django-Oscar.
Oscar has the following structure: there is an application Basket. It has two main classes: Basketand Line. Both classes changed to be used with django.contrib.sites(added fields). Everything worked, that is, different sites have different baskets. But when I put the same products in the basket on different sites, and buy on one of them, then on the second the basket becomes empty.
I got into the Dzhang admin panel, and found out that when you add a product to the cart, aLineand Basketand Lineis associated with Basket. But, when the same product is added to the cart on the second site, it will only be created Lineand it will be linked from Basketthe other site. In other words - one basket for two sites.
How can I change it? How to make sure that the cart is created only on the site where the product was added to it?
Thanks in advance for your help.
Here are examples of models:

class Basket(AbstractBasket):

  site = models.ForeignKey(Site, blank=True, null=True, on_delete=models.CASCADE)

  objects = CurrentSiteManager()

  def __init__(self, *args, **kwargs):
    super(AbstractBasket, self).__init__(*args, **kwargs)

    self._lines = None
    self.offer_applications = results.OfferApplications()
    self.site = Site.objects.get_current()

class Line(AbstractLine):

  site = models.ForeignKey(Site, blank=True, null=True, on_delete=models.CASCADE)

  objects = CurrentSiteManager()

  def __init__(self, *args, **kwargs):
    super(AbstractLine, self).__init__(*args, **kwargs)
    # Instance variables used to persist discount information
    self._discount_excl_tax = D('0.00')
    self._discount_incl_tax = D('0.00')
    self._affected_quantity = 0
    self.site = Site.objects.get_current()

=================UPDATE================
I followed the advice and dug a little, found out that I need to change the managers for the basket. Here's what happened:
class OpenBasketManager(models.Manager):
    """For searching/creating OPEN baskets only."""
    status_filter = "Open"

    def get_queryset(self):
        return super(OpenBasketManager, self).get_queryset().filter(
            status=self.status_filter, site__id = settings.SITE_ID)

    def get_or_create(self, **kwargs):
        return self.get_queryset().get_or_create(
            status=self.status_filter, **kwargs)

I just put site__id = settings.SITE_IDin . And everything worked. But, when I added a product to an empty cart and then on the second site I add the same product to the cart, an error occurs:
IntegrityError at /en-gb/basket/add/15/
duplicate key value violates unique constraint "basket_line_basket_id_7d0d707a7fd92c45_uniq"
DETAIL:  Key (basket_id, line_reference)=(13, 15_11) already exists.

And in the admin panel, Django saw that the status of the cart that I created later was not Open - currently active, but Merged - superceded by another basket. I understand that when I added the product a second time, Django-Oscar considered that I had a basket and merged the two of them.
Can you help with advice? I will be very grateful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oscar Django, 2015-12-14
@MAGistr_MTM

Here you can see that when adding a product, the cart is taken from request.
Here you can see that when placing an order, the basket is again taken from request.
Here the basket is added to request.
Conclusion: it is in the middleware that you need to implement the logic for choosing the current basket.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question