Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

howto add custom form into django administrator page inlines

Can I get a form into Django Administrator page, that I have defined in forms.py? Can I also get this form into Model inlines of Django Administrator page ?

To be clear, this is what I call inline:

class AnswerInline(admin.StackedInline):
  ...
like image 448
user628154 Avatar asked Nov 15 '11 00:11

user628154


People also ask

What is Inlines in Django admin?

The admin interface is also customizable in many ways. This post is going to focus on one such customization, something called inlines. When two Django models share a foreign key relation, inlines can be used to expose the related model on the parent model page. This can be extremely useful for many applications.

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

How do I import a model into admin py?

You can check the admin.py file in your project app directory. If it is not there, just create one. Edit admin.py and add below lines of code to register model for admin dashboard. Here, we are showing all the model fields in the admin site.


1 Answers

Yeah, it's a bit complicated but the docs are actually clear here:

InlineModelAdmin.form
The value for form defaults to ModelForm. This is what is passed through to inlineformset_factory when creating the formset for this inline.

So create your form class and then refer to it in the inline, like so:

class MyForm(forms.ModelForm):
    …

class AnswerInLine(admin.StackedInline):
    form = MyForm
like image 101
Jordan Reiter Avatar answered Sep 20 '22 18:09

Jordan Reiter