django admin foreign key field data add

前端 未结 2 2096
闹比i
闹比i 2020-12-15 17:55

In add form for any app in django admin, for foreign key fields of that model.. comes a dropdown list with add button(which opens in a pop-up). Can we have a form where we c

相关标签:
2条回答
  • 2020-12-15 18:10

    There's a django add-on to get inlines in cases like this where the relationship is the opposite of the usual: django_reverse_admin

    You'll need to add django_reverse_admin to your requirements.txt:

    -e git+https://github.com/anziem/django_reverse_admin.git#egg=django_reverse_admin
    

    Then import it:

    admin.py

    from django_reverse_admin import ReverseModelAdmin
    
    class UserProfileAdmin(ReverseModelAdmin):
        inline_reverse = ['user', 'contact']
        inline_type = 'tabular'  # or could be 'stacked'
    
    admin.site.register(UserProfile, UserProfileAdmin)
    
    0 讨论(0)
  • 2020-12-15 18:23

    Yes, you can do that using the inline admin system.

    class UserAdmin(admin.StackedInline):
        model = User
    class ContactAdmin(admin.StackedInline):
        model = Contact
    
    class UserProfileAdmin(admin.ModelAdmin):
        inlines = [ UserAdmin, ContactAdmin ]
    

    for more details check out https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects .

    0 讨论(0)
提交回复
热议问题