How do I add a custom inline admin widget in Django

0 votes

This is easy for non-inlines. Just override the following in the your admin.py AdminOptions:

def formfield_for_dbfield(self, db_field, **kwargs):
    if db_field.name == 'photo':
        kwargs['widget'] = AdminImageWidget()
        return db_field.formfield(**kwargs)
    return super(NewsOptions,self).formfield_for_dbfield(db_field,**kwargs)

I can't work out how to adapt this to work for inlines. Any Help?

Jun 12, 2020 by kartik
• 37,510 points
1,025 views

1 answer to this question.

0 votes

Hello @kartik,

It works exactly the same way. The TabularInline and StackedInline classes also have a formfield_for_dbfield method, and you override it the same way in your subclass.

Since Django 1.1, formfield_overrides is also working

formfield_overrides = {
    models.ImageField: {'widget': AdminImageWidget},
}

Hope this helps!

answered Jun 12, 2020 by Niroj
• 82,720 points

Related Questions

0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,046 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,564 views
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,720 points
2,757 views
0 votes
1 answer

How do I include custom data in a AWS Cloudwatch Alert?

Since the web service uses the "event ...READ MORE

answered Aug 20, 2018 in DevOps on Cloud by Damon Salvatore
• 5,980 points
149 views
0 votes
1 answer

How to load a custom JS file in Django admin home?

Hello @kartik, You can override templates/admin/index.html and add the JavaScript ...READ MORE

answered May 14, 2020 in Python by Niroj
• 82,720 points
2,552 views
+1 vote
1 answer

How do I create a slug in Django?

Hello @kartik, You will need to use the ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,720 points
456 views