Like in the title I want to swap the field that is rendered on to the form. For example I have the following model:
models.py
class Project(models.Model):
name = models.TextField(max_length=200)
finished = models.BoolenField(default=False)
admin.py
class ProjectAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
#here change the name field form TextField
#to a disabled TextField or to other Field defined by me
self.form['name']['widget] = MySuperDuperWidgetField() #or smth like that
Is it possible?
You will have to override the formfield_for_dbfield method in your ModelAdmin subclass. In it, you can examine the request, the current user, and the object instance, and determine the correct formfield to use (with whatever custom widget you need)
(formfield_for_dbfield is undocumented, so proceed at your own risk :) ) if you know that the field in question is a foreign key, an m2m, or a choice field, then there are documented methods to override for those.