Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

django admin foreign key field data add

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 can add the foreign key model fields in the same form.

For e.g

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    contact = models.ForeignKey(Contact, blank=True, null=True)

For user and contact fields a dropdown with add button is present in admin add form.Can we have all fields of user and contact in same page??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...