In admin_forms.py I've written the following code:
class AdminForm(forms.Form):
category = forms.ModelChoiceField(queryset=Category.objects.all())
question = forms.CharField(widget=forms.Textarea)
q_active = forms.BooleanField(initial=True)
option = forms.CharField()
option_active = forms.BooleanField(initial=True)
I want it get registered with Django Admin, so in my admin.py, I've written
from api.admin_forms import AdminForm
class EntryAdmin(admin.ModelAdmin):
add_form = AdminForm
fieldsets = (
('Category', {
'fields': 'category'}),
('Question', {'fields': ('question', 'q_active')}),
('Answer Option', {'fields': ('option', 'option_active')}),
('Selected Answer', {'fields': ('user_role', 'answer')}),
)
admin.site.register(EntryAdmin)
Definitely, this is not how we can make it work. help please!
To automate this process, we can programmatically fetch all the models in the project and register them with the admin interface. Open admin.py file and add this code to it. This will fetch all the models in all apps and registers them with the admin interface.
One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.
You should set form
instead of add_form
.
class EntryAdmin(admin.ModelAdmin):
form = AdminForm
...
When you register your model admin, you must provide the model as the first argument:
admin.site.register(Entry, EntryAdmin)
ModelAdmin
does not have an attribute add_form
, so setting it has no effect. The UserAdmin
has an add_form
attribute, which is used when adding new users.
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