What is an easy way to implement one-many editing in list_editable in django admin?

Given the following models:

class Store(models.Model):
    name = models.CharField(max_length=150)

class ItemGroup(models.Model):
    group = models.CharField(max_length=100)
    code = models.CharField(max_length=20)

class ItemType(models.Model):
    store = models.ForeignKey(Store, on_delete=models.CASCADE, related_name="item_types")
    item_group = models.ForeignKey(ItemGroup)
    type = models.CharField(max_length=100)

Built-in descriptor, adding a few item_typesto Storebeautifully when viewing a single Store.

The content administration team would like to be able to edit stores and their types in bulk. Is there an easy way to implement Store.item_typesin list_editable, which also allows you to add new entries similar to horizontal_filter? If not, is there a simple reference showing how to implement a custom template list_editable? I was googling, but could not come up with anything.

In addition, if there is an easier or better way to customize these models that would simplify its implementation, feel free to comment.

+5
1

ItemType ManyToManyField ?

, ItemTypes, , Store ( ItemType).

:.

from django.db import models

class ItemGroup(models.Model):
    group = models.CharField(max_length=100)
    code = models.CharField(max_length=20)

class ItemType(models.Model):
    item_group = models.ForeignKey(ItemGroup)
    type = models.CharField(max_length=100)

class Store(models.Model):
    name = models.CharField(max_length=150)
    item_type = models.ManyToManyField(ItemType, related_name="store")

# admin
from django.contrib import admin

class StoreAdmin(admin.ModelAdmin):
    list_display=('name', 'item_type',)
    list_editable=('item_type',)

for model in [(Store, StoreAdmin), (ItemGroup,), (ItemType,)]:
    admin.site.register(*model)

:

File "C:\Python27\lib\site-packages\django\contrib\admin\validation.py", line 43, in validate
% (cls.__name__, idx, field))
django.core.exceptions.ImproperlyConfigured: 'StoreAdmin.list_display[1]', 'item_type' is a ManyToManyField which is not supported.

, 41-43 django.contrib.admin.validation:

#if isinstance(f, models.ManyToManyField):
#    raise ImproperlyConfigured("'%s.list_display[%d]', '%s' is a ManyToManyField which is not supported."
#        % (cls.__name__, idx, field))

, , , , .

+1

All Articles