I have implemented search and filtering for Post class in Django admin panel. Post class has no password filed but a password field appeared in admin panel. I want to remove it from admin panel.
# admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.db import models
from .models import Post, Comment
class PostAdmin(UserAdmin):
ordering = ('created_at',)
list_display = ('author', 'blood_group', 'required_bags', 'contact_number', 'created_at', 'is_resolved', 'admin_approved')
search_fields = ('author__name', 'blood_group', 'is_resolved',) # Can not add author as it is a foregin key to post
readonly_fields = ('created_at',)
exclude = ('password',)
filter_horizontal = ()
list_filter = ()
fieldsets = ()
admin.site.register(Post, PostAdmin)
admin.site.register(Comment)
I have tried to remove the password field using exclude = ('password',)
in admin.py of post app. It worked for my User model that actually had password field. But it is not working for Post model.
Here is code for forms.py
# forms.py
from django.forms import ModelForm
from .models import Post, Comment
from django import forms
class PostForm(ModelForm):
class Meta:
model = Post
fields = ['description', 'address', 'blood_group', 'required_bags', 'deadlineDate', 'deadlineTime', 'contact_number', 'is_resolved']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field, in self.fields.items():
field.widget.attrs.update({'class' : 'form-control'})
self.fields['deadlineDate'].widget.input_type = 'date'
self.fields['deadlineTime'].widget.input_type = 'time'
self.fields['contact_number'].widget.input_type = 'number'
self.fields['description'].widget.attrs.update({'rows' : '4'})
self.fields['is_resolved'].widget.attrs.update({'class' : 'form-check-input'})
Here is code for modes.py
# models.py
from django.db import models
from author.decorators import with_author
BLOOD_GROUPS = [
('A ', 'A '),
('A-', 'A-'),
('B ', 'B '),
('B-', 'B-'),
('AB ', 'AB '),
('AB-', 'AB-'),
('O ', 'O '),
('O-', 'O-'),
]
@with_author
class Post(models.Model):
description = models.TextField(null=True, blank=True)
address = models.CharField(max_length=250)
blood_group = models.CharField(max_length=8, choices=BLOOD_GROUPS)
required_bags = models.PositiveIntegerField()
deadlineDate = models.DateField()
deadlineTime = models.TimeField()
contact_number = models.CharField(max_length=15)
created_at = models.DateTimeField(auto_now_add=True)
is_resolved = models.BooleanField(default=False, blank=True)
admin_approved = models.BooleanField(default=False)
def __str__(self):
return str(self.author) ", " str(self.blood_group) ", " str(self.address)
CodePudding user response:
It seems to me like you should inherit PostAdmin
from ModelAdmin
, not UserAdmin
since your Post
model is not connected to the User
model in any way.
# forms.py
from django.contrib.admin import ModelAdmin
from django.forms import ModelForm
from .models import Post, Comment
from django import forms
class PostForm(ModelAdmin):
class Meta:
model = Post
fields = ['description', 'address', 'blood_group', 'required_bags', 'deadlineDate', 'deadlineTime', 'contact_number', 'is_resolved']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field, in self.fields.items():
field.widget.attrs.update({'class' : 'form-control'})
self.fields['deadlineDate'].widget.input_type = 'date'
self.fields['deadlineTime'].widget.input_type = 'time'
self.fields['contact_number'].widget.input_type = 'number'
self.fields['description'].widget.attrs.update({'rows' : '4'})
self.fields['is_resolved'].widget.attrs.update({'class' : 'form-check-input'})
The UserAdmin
is for showing the admin page of the User
model specifically.