简体   繁体   中英

Adding fields from another model into admin inlines - Django admin

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM