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,520 points
1,549 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,840 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,673 views
0 votes
1 answer
+5 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,790 points
4,461 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,840 points
10,939 views
0 votes
1 answer

How do I add a custom login and logout page in my Tableau Server on-prem?

I don't believe it is "officially" possible ...READ MORE

answered Mar 4, 2022 in Tableau by Neha
• 9,020 points
836 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 21, 2018 in DevOps on Cloud by Damon Salvatore
• 5,980 points
1,043 views
0 votes
1 answer

How do I add a column in ALTER TABLE in Hive?

Hi, Yes, we can add column inside a ...READ MORE

answered May 15, 2019 in Big Data Hadoop by Gitika
• 65,770 points
56,106 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP