Skip to content

Commit

Permalink
Fixed: recipe ingredients sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Oct 6, 2023
1 parent baf09f1 commit 83062a2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions bread/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"recipes.apps.RecipesConfig",
"api.apps.ApiConfig",
"rest_framework",
"adminsortable2",
]

if DEBUG:
Expand Down
7 changes: 5 additions & 2 deletions recipes/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
),
]
8 changes: 4 additions & 4 deletions recipes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "ингредиент рецепта"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 83062a2

Please sign in to comment.