ImageField in admin always required

129 views
Skip to first unread message

Detectedstealth

unread,
Dec 6, 2012, 5:47:57 PM12/6/12
to django...@googlegroups.com
Hi,

I have a picture for my custom user declared as follows:

class CustomUser(AbstractBaseUser):
    picture = models.ImageField(upload_to='profile_pictures', null=True, blank=True)

In admin I use the image

class CustomUserAdmin(UserAdmin):
    fieldsets(
        ('Profile details', {
            'fields': (
                'picture',
                'status',
            )
        }),
    )

Now for some reason even though I have null=True, blank=True in admin the picture turns into a required field. Has anyone else experienced this or is this expected behaviour ? 

Detectedstealth

unread,
Dec 12, 2012, 3:11:10 AM12/12/12
to django...@googlegroups.com
Update:

Looks like it was an issue with the admin.py file custom code, I was following a code sample to display images inline which is causing the field to become required. Any suggestions on how to fix this? (See code below) 

class AdminImageFieldWithThumbWidget(forms.widgets.FileInput):

  def __init__(self, thumb_width=50, thumb_height=50):

    self.width = thumb_width

    self.height = thumb_height

    super(AdminImageFieldWithThumbWidget, self).__init__({})


  def render(self, name, value, attrs=None):

    thumb_html = ''

    if value and hasattr(value, 'url'):

      print(value)

      thumb_html = '<img src="/static/%s" width="%s" height="%s"/>' % (value.url, self.width, self.height)

    return mark_safe("%s%s" % (thumb_html, super(AdminImageFieldWithThumbWidget, self).render(name, value, attrs)))


class CustomUserAdmin(UserAdmin):

  def formfield_for_dbfield(self, db_field, **kwargs):

    if db_field.name == 'picture':

      return forms.ImageField(widget=AdminImageFieldWithThumbWidget(thumb_width = self.thumb_width, thumb_height = self.thumb_height))

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


NOTE: I tried adding null=True, blank=True to forms.ImageField however I get errors __init__() got an unexpected keyword argument so I guess I can't use them options. If I comment out the def formfield_for_dbfield(self, db_field, **kwargs): then the ImageField is not required as expected.

Chris Cogdon

unread,
Dec 12, 2012, 4:39:36 PM12/12/12
to django...@googlegroups.com


NOTE: I tried adding null=True, blank=True to forms.ImageField however I get errors __init__() got an unexpected keyword argument so I guess I can't use them options. If I comment out the def formfield_for_dbfield(self, db_field, **kwargs): then the ImageField is not required as expected.

"ImageField is not required as expected" Isn't that what you want? :)

Anyway, the attribute for allowing a FormField to be "left blank" is required=False... blank=True and null=True are for ModelFields

If that knowledge doesn't give you any joy, try constructing a simple model that is not inherited from AbstractBaseUser... does that still end up having the picture be required?


Detectedstealth

unread,
Dec 16, 2012, 9:10:08 PM12/16/12
to django...@googlegroups.com
Thanks required=False is what I needed :). 

"ImageField is not required as expected" Isn't that what you want? :) Yes that is exactly what I wanted but also wanted to display the actual <img> setting the required=False allows me to have my cake and eat it too :). Not sure why required vs blank, null slipped my mind.

PS: It gives great JOY :D
Reply all
Reply to author
Forward
0 new messages