forms.py
First we need to create our model form which will be used to input data in the table.
Create forms.py in your djangoapp folder, import the model and a built-in django form system.
The class ComputerForm will create a model form that can be called when you need it.
from django import forms
from .models import Computer
class ComputerForm(forms.ModelForm):
class Meta:
model = Computer
fields = ['computer_name', 'IP_address', 'MAC_address', 'users_name', 'location']
admin.py
Now we need to call that form in the admin and customize how our Table will be displayed in the admin
from .forms import ComputerForm
class ComputerAdmin(admin.ModelAdmin):
list_display = ["computer_name", "IP_address", "MAC_address"]
form = ComputerForm
list_filter = ['computer_name', 'IP_address']
search_fields = ['computer_name', 'IP_address']
Modify your registration to apply the customization.
admin.site.register(Computer, ComputerAdmin)
Hi Arbadjie,
Thanks for your beautiful tutorial. I was following your videos duirng learning Django. I have a question here. Why do we need to import ComputerForm in Admin.py. As we never used anywhere. Even I removed the lines from my Admin.py files and it is working fine for me.
So my admin.py file is now:
class ComputerAdmin(admin.ModelAdmin):
list_display = [“computer_name”, “IP_address”, “MAC_address”]
list_filter = [‘computer_name’, ‘IP_address’]
search_fields = [‘computer_name’, ‘IP_address’]
Please let me know the reason to import the ComputerForm in Admin.py.
regards.
You will need the form when you want to use a custom form when inputting data using the admin portal. Probably you have this => from .forms import *
That will import all the forms in forms.py and with that, you will not need to individually import the ComputerForm.
Looking at your code, you are not using the form in your ComputerAdmin class, therefore, you will be using the whole fields in the Computer model as your form fields.
Lets say in your models you have these fields (computer_name, Ip_address, and Mac_address)
but you want to see only computer_name and Ip_address when inputing data. Then you will need to create a form in forms.py and give it only the two fields (computer_name and Ip_address) then you will now import add the form in your ComputerAdmin class in admin.py
Hi Arbadjie,
Thank for this tutorial, I am having an indentation error when I copy this
class ComputerForm(forms.ModelForm):
class Meta:
model = Computer
fields = [‘computer_name’, ‘IP_address’, ‘MAC_address’, ‘users_name’, ‘location’]
question 1:
is class Meta indented below class ComputerForm or are they in the same line? can you email me the code for this section bro so I can see the right indentation. I am currently stuck in this section bro. thanks
This is normal and common when copying and pasting codes.
You gonna select a few lines of codes where the error is pointing.
You will see some lines of code will have periods like this >….. before the code while some will have dashes like this > —– before the code.
You will need to have periods of dashes on codes that should have the same indentation.
You can choose…. Or —-
Anyone should work.
Hi bro I already solved this issue. Thanks
hi arbadje,
how can i make it as multistepform?
I have not covered that on the channel yet. I guess I will do this as it is a nice feature.
I will go with jQuery to hide some fields and unhide when I click next.