[英]What's the straightforward way to implement one to many editing in list_editable in django admin?
鑒於以下型號:
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)
內嵌的手柄添加多個item_types
到Store
觀看一個很好時, Store
。
內容管理員團隊希望能夠批量編輯商店及其類型。 有沒有實現一個簡單的方法Store.item_types
在list_editable
這也允許添加新的記錄,類似horizontal_filter
? 如果沒有,是否有一個簡單的指南,說明如何實現自定義list_editable
模板? 我一直在谷歌搜索,但一直無法想出任何東西。
此外,如果有一種更簡單或更好的方法來設置這些模型,以便更容易實現,請隨意發表評論。
如何使ItemType成為Store的ManyToManyField?
對我來說,似乎合乎邏輯的是,如果您要更改商店中可用的ItemType,您將更改商店的屬性(而不是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.
我通過在django.contrib.admin.validation中注釋掉第41-43行解決了這個問題:
#if isinstance(f, models.ManyToManyField):
# raise ImproperlyConfigured("'%s.list_display[%d]', '%s' is a ManyToManyField which is not supported."
# % (cls.__name__, idx, field))
可能不是理想的解決方案,但它似乎對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.