4

How can I get a model's fields to be editable in the Inline in a ManyToMany relationship when the relationship is with itself?

class MyModel(models.Model):
    children = models.ManyToManyField(
        'self',
        related_name='parents',
        symmetrical=False
    )
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

@admin.register(MyModel)
class MyModelAdmin(VersionAdmin):
    inlines = (MyModelChildAdminInline, )
    exclude = ('children', )
everspader
  • 1,272
  • 14
  • 44

0 Answers0