diff --git a/bread/settings.py b/bread/settings.py index 43b8388..46ead0b 100644 --- a/bread/settings.py +++ b/bread/settings.py @@ -57,6 +57,7 @@ "recipes.apps.RecipesConfig", "api.apps.ApiConfig", "rest_framework", + "adminsortable2", ] if DEBUG: diff --git a/recipes/admin/admin.py b/recipes/admin/admin.py index e37b0a0..aa4ceb7 100644 --- a/recipes/admin/admin.py +++ b/recipes/admin/admin.py @@ -3,6 +3,8 @@ from django.contrib import admin from django.http import HttpRequest +from adminsortable2.admin import SortableTabularInline, SortableAdminBase + from ..models import * from .actions import comment_accept, comment_reject @@ -18,15 +20,16 @@ class RecipeCommentInline(admin.TabularInline): ordering = ("-created_at",) -class RecipeIngredientAdminInline(admin.TabularInline): +class RecipeIngredientAdminInline(SortableTabularInline): model = RecipeIngredient + ordering = ["position"] def get_queryset(self, request): return super().get_queryset(request).select_related("ingredient", "measure") @admin.register(Recipe) -class RecipeAdmin(admin.ModelAdmin): +class RecipeAdmin(SortableAdminBase, admin.ModelAdmin): list_filter = ("category",) search_fields = ("name",) inlines = (RecipeIngredientAdminInline, RecipeCommentInline) diff --git a/recipes/migrations/0012_remove_recipeingredient_unique_recipeingredient_position_and_more.py b/recipes/migrations/0012_remove_recipeingredient_unique_recipeingredient_position_and_more.py new file mode 100644 index 0000000..d650e48 --- /dev/null +++ b/recipes/migrations/0012_remove_recipeingredient_unique_recipeingredient_position_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 4.1.10 on 2023-10-06 16:04 + +from django.db import migrations, models +import recipes.storage + + +class Migration(migrations.Migration): + dependencies = [ + ("recipes", "0011_alter_comment_options_alter_category_created_at_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="recipeingredient", + name="position", + field=models.PositiveIntegerField(verbose_name="№"), + ), + migrations.AddIndex( + model_name="recipeingredient", + index=models.Index( + fields=["recipe", "position"], name="idx_recipeingredient_position" + ), + ), + migrations.RemoveConstraint( + model_name="recipeingredient", + name="unique_recipeingredient_position", + ), + ] diff --git a/recipes/models.py b/recipes/models.py index 25d4d28..6b442aa 100644 --- a/recipes/models.py +++ b/recipes/models.py @@ -234,10 +234,10 @@ def __str__(self): return f"{self.quantity} {self.measure.name} {self.ingredient.name}" class Meta: - constraints = [ - models.UniqueConstraint( - fields=["recipe", "position"], name="unique_recipeingredient_position" - ) + indexes = [ + models.Index( + fields=["recipe", "position"], name="idx_recipeingredient_position" + ), ] ordering = ["recipe", "position"] verbose_name = "ингредиент рецепта" diff --git a/requirements.txt b/requirements.txt index 3c53e03..d9b1733 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ Django==4.1.10 Pillow==10.0.1 azure-storage-blob==12.13.0 boto3==1.26.24 +django-admin-sortable2==2.1.10 django-storages==1.12.3 djangorestframework==3.14.0 mysqlclient==2.1.1