-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from b2b-marketplace/feature/basket_model
Модель корзины
- Loading branch information
Showing
8 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
), | ||
] |
31 changes: 31 additions & 0 deletions
31
apps/baskets/migrations/0002_basket_basket_products_alter_basketproduct_basket.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters