0

Consider this:

@admin.register(MyModel)
MyModelAdmin(admin.ModelAdmin):

    fieldsets = [
        ('Field1', {'fields': ['field1']}),
        ('Field2', {'fields': ['field2']}),
        ...
        ]
    list_display = ('field1', 'field2')

Let's say I have another model for my admin:

@admin.register(AnotherModel)
AnotherModelAdmin(admin.ModelAdmin):

    fieldsets = [
        ('Field1', {'fields': ['field3']}),
        ('Field2', {'fields': ['field4']}),
        ...
        ]
    list_display = ('field3', 'field4')

So, I need to show field3 from AnotherModelAdmin into MyModelAdmin inlines.

How can I achieve this?

NeoVe
  • 3,857
  • 8
  • 54
  • 134
  • Here is how you should do it: https://stackoverflow.com/a/14778274/186202 – Natim Mar 11 '19 at 22:37
  • Possible duplicate of [How do I call a model method in django ModelAdmin fieldsets?](https://stackoverflow.com/questions/14777989/how-do-i-call-a-model-method-in-django-modeladmin-fieldsets) – Natim Mar 11 '19 at 22:37
  • But this is not a method is a different class – NeoVe Mar 11 '19 at 22:55
  • 1
    You can create a function that would display the field of the other model, what would prevent it? – Natim Mar 12 '19 at 15:09
  • Hi, thank you, I was just thinking about that, but there are many ways to accomplish it, I'm a little thick in my mind, do you have any simple example that works for you? – NeoVe Mar 12 '19 at 15:19

1 Answers1

1

What about something like that?

class AddressAdmin(admin.ModelAdmin):
    fieldsets = [("User", {'fields': ['user_address']}),]
    readonly_fields = ['user_address']

    def user_address(self, obj):
        return obj.user.address
    user_address.short_description = 'User address'

Or maybe you are want to actually change the content of the another model field. In that case you can use something like that: How to create a UserProfile form in Django with first_name, last_name modifications?

Natim
  • 17,274
  • 23
  • 92
  • 150