0

I’m overriding fields in Django admin like this:

class MyPageAdmin(FlatPageAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'url':
            return db_field.formfield(widget=forms.TextInput(attrs={'size': 100}))

        if db_field.name == 'title':
            return db_field.formfield(widget=forms.TextInput(attrs={'size': 150}))

        return super(MyPageAdmin, self).formfield_for_dbfield(db_field, **kwargs)

admin.site.unregister(FlatPage)
admin.site.register(MyPage, MyPageAdmin)

Now, the problem is that for the "Title" field in Django admin, the field width changes just as expected – size attribute is set to 150, but for the URL field, there’s absolutely now change. Yet, the code does run into the specific if statement, so the URL field is definitely there, but for the life of me, I cannot figure out, why only this field is unaffected by the override. Other fields do not suffer this problem (I have other overrides besides the "Title" field, but only the URL is stuck in its original size).

margusholland
  • 3,306
  • 1
  • 23
  • 30
  • inspect the page dom, and ensure that it was set as expected, if so, in same case, maybe the value of size is overrided by css which is generated dynamically by js. – Blackdoor Jul 14 '20 at 23:56
  • That’s the problem: the DOM doesn’t change. For one field it does, for the other it doesn’t. – margusholland Jul 17 '20 at 07:09

1 Answers1

0

I doubt that the style changes applied in Django will ever get passed on to the template. Here are three things you can try:

  1. attrs={'size': '100 !important'} The !important tag in CSS basically means to override all other styles whether they are from the parent or children (but still remember to pass in the quotation marks), although I'm unsure whether this worked in Django as it worked in React.

  2. Try applying a class name to the division and then override manually in the template or via another CSS file. This is an alternative to the first method in case the syntax doesn't allow you to pass in non-numeric attributes.

  3. If all the above methods don't work, I'm sorry, but you might've need to design your own template. Please read more about custom templates via this thread.

If you're wondering how's the second method different from the third, the second one is simply overriding some styles and the third method is replacing the entire template. The third one will definitely work if you're in a hurry.

crimsonpython24
  • 2,223
  • 2
  • 11
  • 27