Skip to content

Commit

Permalink
Merge pull request #27 from b2b-marketplace/feature/basket_model
Browse files Browse the repository at this point in the history
Модель корзины
  • Loading branch information
Rikworkco authored Aug 13, 2023
2 parents 6dd596d + b57754e commit 1592ed7
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 0 deletions.
Empty file added apps/baskets/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions apps/baskets/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib import admin

from apps.baskets.models import Basket, BasketProduct


class BasketProductInline(admin.TabularInline):
model = BasketProduct
extra = 1


@admin.register(Basket)
class BasketAdmin(admin.ModelAdmin):
list_display = ("user",)
list_filter = ("user",)
search_fields = ("user",)
empty_value_display = "-empty-"
inlines = (BasketProductInline,)
6 changes: 6 additions & 0 deletions apps/baskets/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BasketsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.baskets"
84 changes: 84 additions & 0 deletions apps/baskets/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Generated by Django 4.1 on 2023-08-11 19:26

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [
("products", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Basket",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="user",
to=settings.AUTH_USER_MODEL,
verbose_name="User",
),
),
],
options={
"verbose_name": "Basket",
"verbose_name_plural": "Baskets",
},
),
migrations.CreateModel(
name="BasketProduct",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"quantity",
models.PositiveIntegerField(verbose_name="Product quantity"),
),
(
"basket",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="basket",
to="baskets.basket",
verbose_name="Basket",
),
),
(
"product",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="product",
to="products.product",
verbose_name="Product",
),
),
],
options={
"verbose_name": "Product in the basket",
"verbose_name_plural": "Products in the basket",
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.1 on 2023-08-12 20:28

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("products", "0001_initial"),
("baskets", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="basket",
name="basket_products",
field=models.ManyToManyField(
through="baskets.BasketProduct", to="products.product"
),
),
migrations.AlterField(
model_name="basketproduct",
name="basket",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="basket",
to="baskets.basket",
verbose_name="Basket",
),
),
]
Empty file.
36 changes: 36 additions & 0 deletions apps/baskets/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.contrib.auth import get_user_model
from django.db import models
from django.utils.translation import gettext_lazy as _

from apps.products.models import Product


class Basket(models.Model):
user = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="user", verbose_name=_("User")
)
basket_products = models.ManyToManyField(Product, through="BasketProduct")

class Meta:
verbose_name = _("Basket")
verbose_name_plural = _("Baskets")

def __str__(self):
return f"{self.user}"


class BasketProduct(models.Model):
basket = models.ForeignKey(
Basket, on_delete=models.CASCADE, related_name="basket", verbose_name=_("Basket")
)
product = models.ForeignKey(
Product, on_delete=models.CASCADE, related_name="product", verbose_name=_("Product")
)
quantity = models.PositiveIntegerField(verbose_name=_("Product quantity"))

class Meta:
verbose_name = _("Product in the basket")
verbose_name_plural = _("Products in the basket")

def __str__(self):
return f"{self.quantity} x {self.product} in Basket for User: {self.basket.user}"
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"apps.products",
"apps.users",
"apps.orders",
"apps.baskets",
]

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
Expand Down

0 comments on commit 1592ed7

Please sign in to comment.