eve*_*der 5 django django-models django-forms django-admin
Inline当关系与其自身存在时,如何才能在多对多关系中使模型的字段可编辑?
class MyModel(models.Model):
children = models.ManyToManyField(
'self',
related_name='parents',
symmetrical=False
)
Run Code Online (Sandbox Code Playgroud)
class MyModelChildAdminInline(admin.TabularInline):
"""Inline for the intermediate table"""
model = MyModel.children.through
# Number of child-forms shown at the bottom of the Profile detail view
extra = 0
fk_name = 'from_mymodel'
raw_id_fields = ("to_mymodel",)
fields = ('to_mymodel', 'field1', 'field2', 'field3', )
readonly_fields = ('field1', 'field2', 'field3',)
def field1(self, obj):
return obj.to_mymodel.field1
def field2(self, obj):
return obj.to_mymodel.field3
def field3(self, obj):
return obj.to_mymodel.field2
Run Code Online (Sandbox Code Playgroud)
@admin.register(MyModel)
class MyModelAdmin(VersionAdmin):
inlines = (MyModelChildAdminInline, )
exclude = ('children', )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
348 次 |
| 最近记录: |