Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 808
Neo Avatar asked Jun 27 '11 11:06

Neo


People also ask

How does Django store foreign key values?

Note that the _id in the artist parameter, Django stores foreign keys id in a field formed by field_name plus _id so you can pass the foreign key id directly to that field without having to go to the database again to get the artist object.

What is Fieldsets in Django admin?

fieldsets is a list of two-tuples, in which each two-tuple represents a <fieldset> on the admin form page. (A <fieldset> is a “section” of the form.)

How do you make a field required in Django admin?

Making Fields Required In Django Admin In order to make the summary field required, we need to create a custom form for the Post model. I am making them on the same file you can do this on a separate forms.py file as well.


1 Answers

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 .

like image 51
Darioush Avatar answered Sep 20 '22 12:09

Darioush