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):
...
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.
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With