[Fixed]-Many to many in django Admin

1๐Ÿ‘

I have done this job. So this is my code:

admin.py

from django.contrib import admin

from myapp.models import Client, Item, Order, Ordering

class ClientAdmin(admin.ModelAdmin):
    list_display = ("f_name", "l_name", "m_name", "phone", "city", "address")
    list_filter = ["f_name", "l_name", "city"]

class ItemAdmin(admin.ModelAdmin):
    list_display = ("name", "type_item", "color", "cost", "is_available_now",
                    "available_count")
    list_filter = ["name", "type_item", "color", "cost", "available_count"]

class OrderingAdmin(admin.ModelAdmin):
    list_display = ("id_order", "id_item", "count_items")
    list_filter = ["id_order", "id_item", "count_items"]

class OrderingInline(admin.TabularInline):
    model = Ordering
    fk_name = "id_order"
    max_num = 1

class OrderAdmin(admin.ModelAdmin):
    list_display = ("id", "id_client", "date_order", "date_taken", "is_paid",
                    "is_taken")
    list_filter = ["date_order", "date_taken", "is_paid", "is_taken"]

    inlines = [
        OrderingInline,
        ]

admin.site.register(Item, ItemAdmin)
admin.site.register(Order, OrderAdmin)
admin.site.register(Client, ClientAdmin)
admin.site.register(Ordering, OrderingAdmin)

And what I take:

enter image description here

Thanks to all)))

๐Ÿ‘คOleg Klimenko

0๐Ÿ‘

Instead of use Order_item class, you should add a items = models.ManyToManyField(Item) field into your current Order class.

Leave a comment