Add ShopModel and integrate with ProductModel

- Created ShopModel with fields for shop name, description, and user association.
- Updated ProductModel to include a foreign key relationship with ShopModel.
- Added admin interface for ShopModel with search functionality.
- Created migrations for ShopModel and its integration with ProductModel.
This commit is contained in:
Parsa Nazer
2025-08-02 18:57:46 +03:30
parent d955fcb107
commit d0dfa3eaa7
7 changed files with 115 additions and 4 deletions
@@ -0,0 +1,28 @@
# Generated by Django 5.1.2 on 2025-08-02 15:21
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0028_newsmodel_is_read'),
]
operations = [
migrations.CreateModel(
name='ShopModel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('shop_name', models.CharField(max_length=100, verbose_name='نام فروشگاه')),
('shop_description', models.TextField(verbose_name='توضیحات فروشگاه')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='shop', to=settings.AUTH_USER_MODEL, verbose_name='کاربر')),
],
options={
'verbose_name': 'فروشگاه',
'verbose_name_plural': 'فروشگاه ها',
},
),
]
+14
View File
@@ -120,6 +120,20 @@ class User(AbstractBaseUser, PermissionsMixin):
return self.phone
class ShopModel(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='shop', verbose_name='کاربر')
shop_name = models.CharField(max_length=100, verbose_name='نام فروشگاه')
shop_description = models.TextField(verbose_name='توضیحات فروشگاه')
def __str__(self):
return f"{self.user.phone} - {self.shop_name}"
class Meta:
verbose_name = 'فروشگاه'
verbose_name_plural = 'فروشگاه ها'
class UserAddressModel(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='address', verbose_name='کاربر')
name = models.CharField(max_length=30, verbose_name='نام ادرس')
+5
View File
@@ -107,6 +107,11 @@ UNFOLD = {
"link": reverse_lazy("admin:order_ordermodel_changelist"),
"badge": "utils.admin.admin_pending_count",
},
{
"title": _("فروشگاه ها"),
"icon": "storefront",
"link": reverse_lazy("admin:account_shopmodel_changelist"),
},
],
},
+14 -2
View File
@@ -41,6 +41,18 @@ class InPackItemsAdmin(ModelAdmin, ImportExportModelAdmin):
"widget": ArrayWidget,
}
}
from account.models import ShopModel
@admin.register(ShopModel)
class ShopModelAdmin(ModelAdmin, ImportExportModelAdmin):
search_fields = ['name']
compressed_fields = True
warn_unsaved_form = True
formfield_overrides = {
ArrayField: {
"widget": ArrayWidget,
}
}
class AttributeValueInLine(StackedInline):
@@ -180,13 +192,13 @@ class ProductModelAdmin(ModelAdmin, ImportExportModelAdmin):
readonly_fields = ('slug', 'created_at')
search_fields = ['name', 'description', ]
list_filter = ['show', 'category']
autocomplete_fields = ['related_products', ]
autocomplete_fields = ['related_products', 'shop']
# compressed_fields = True
warn_unsaved_form = True
actions_list = ['redirect_to_learn', 'update_products_price']
list_display = ['display_image', 'display_price', 'view', 'show', 'rating', 'category', 'created_at']
fieldsets = (
('فیلد های اصلی', {'fields': ('name', 'description', 'category', 'related_products', 'show',), "classes": ["tab"],}),
('فیلد های اصلی', {'fields': ('name', 'description', 'category', 'related_products', 'show', 'shop'), "classes": ["tab"],}),
('فیلد های سيو', {'fields': ('meta_description', 'meta_keywords', 'meta_rating', 'slug'), "classes": ["tab"],}),
('فیلد های مربوط به کاربر', {'fields': ('rating', 'view',), "classes": ["tab"],}),
# ('فیلد های ایتم های پک', {'fields': ('in_pack_items', ), "classes": ["tab"],})
@@ -0,0 +1,33 @@
# Generated by Django 5.1.2 on 2025-08-02 15:21
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('product', '0050_commentmodel_comment_product_idx_and_more'),
]
operations = [
migrations.RemoveIndex(
model_name='productvariant',
name='variant_slider_category_idx',
),
migrations.RemoveIndex(
model_name='productvariant',
name='variant_slider_stock_idx',
),
migrations.RemoveIndex(
model_name='productvariant',
name='variant_slider_discount_idx',
),
migrations.RemoveIndex(
model_name='productvariant',
name='variant_slider_created_idx',
),
migrations.RemoveIndex(
model_name='productvariant',
name='variant_currency_idx',
),
]
@@ -0,0 +1,20 @@
# Generated by Django 5.1.2 on 2025-08-02 15:23
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0029_shopmodel'),
('product', '0051_remove_productvariant_variant_slider_category_idx_and_more'),
]
operations = [
migrations.AddField(
model_name='productmodel',
name='shop',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='products', to='account.shopmodel', verbose_name='فروشگاه'),
),
]
+1 -2
View File
@@ -122,7 +122,7 @@ class ProductModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True, verbose_name='زمان ثبت محصول')
category = models.ForeignKey(SubCategoryModel, null=True, on_delete=models.SET_NULL, related_name='products', verbose_name='دسته بندی محصول')
related_products = models.ManyToManyField('self', blank=True, verbose_name='محصولات مرتبط')
shop = models.ForeignKey('account.ShopModel', on_delete=models.CASCADE, related_name='products', verbose_name='فروشگاه', blank=True, null=True)
@@ -287,7 +287,6 @@ class ProductVariant(models.Model):
verbose_name = 'تنوع محصول'
verbose_name_plural = 'تنوع‌های محصول'
indexes = [
# Existing indexes
models.Index(fields=['product', 'price', 'created_at'], name='idx_product_price_created'),
models.Index(fields=['in_stock', 'discount'], name='idx_stock_discount'),
models.Index(fields=['created_at'], name='idx_created'),