141👍
Create a custom ModelForm for your ModelAdmin and add ‘widgets’ to its Meta class, like so:
class StopAdminForm(forms.ModelForm):
class Meta:
model = Stop
widgets = {
'field_name': ApproveStopWidget(),
}
fields = '__all__'
class StopAdmin(admin.ModelAdmin):
form = StopAdminForm
Done!
Documentation for this is sort of non-intuitively placed in the ModelForm docs, without any mention to it given in the admin docs. See: Creating forms from models
36👍
After digging into the admin, model field and form field code, I believe the only way to carry out what I want is by creating a custom model field:
models.py
from django.db import models
from widgets import ApproveStopWidget
class ApproveStopModelField(models.DateTimeField):
pass
class Stop(models.model):
# Other fields
approve_ts = ApproveStopModelField('Approve place', null=True, blank=True)
admin.py
from widgets import ApproveStopWidget
from models import ApproveStopModelField
class StopAdmin(admin.ModelAdmin):
formfield_overrides = {
ApproveStopModelField: {'widget': ApproveStopWidget }
}
It gets the job done.
For the time being, I’ll leave the question unanswered because I have the habit of missing the obvious. Perhaps some Django smartypants has a better solution.
- [Django]-How to redirect with post data (Django)
- [Django]-Getting a count of objects in a queryset in Django
- [Django]-How to strip html/javascript from text input in django
29👍
Override formfield_for_dbfield like thus:
class VehicleAdmin(admin.ModelAdmin):
search_fields = ["name", "colour"]
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'colour':
kwargs['widget'] = ColourChooserWidget
return super(VehicleAdmin, self).formfield_for_dbfield(db_field,**kwargs)
(credit to http://www.kryogenix.org/days/2008/03/28/overriding-a-single-field-in-the-django-admin-using-newforms-admin/ )
- [Django]-Django: FloatField or DecimalField for Currency?
- [Django]-Django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
- [Django]-What does this Django regular expression mean? `?P`
7👍
Django’s ModelAdmin.get_changelist_form(self, request, **kwargs) will do the trick for the case of list_editable
class StopAdminForm(forms.ModelForm):
class Meta:
model = Stop
widgets = {
'approve_ts': ApproveStopWidget(),
}
class StopAdmin(admin.ModelAdmin):
form = StopAdminForm
#just return the ModelForm class StopAdminForm
def get_changelist_form(self, request, **kwargs):
return StopAdminForm
Refer to Django Official documentation on this topic
I hope this will help
- [Django]-How exactly do Django content types work?
- [Django]-Django – how to unit test a post request using request.FILES
- [Django]-How do I convert a Django QuerySet into list of dicts?
0👍
You can change the widget for only one field by assigning your widget to a field in a custom form and assigning the custom form to an admin as shown below:
# "admin.py"
class ProductForm(forms.ModelForm):
class Meta:
model = Product
widgets = {
'price': PriceWidget(),
}
fields = '__all__'
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
form = ProductForm
- [Django]-Django REST framework: non-model serializer
- [Django]-How can I check the size of a collection within a Django template?
- [Django]-Cannot import name patterns