16

Is there an easy way to include file upload capabilities to the admin interface in Django? I saw this question but I'm not well versed in Javascript.

Is there any magick I can add to the models.py or admin.py files that will allow me to easily do this with Django's built in CMS system?

Background:

I'm trying to create a database of celebrities that include their bio, birth dates and I want to include a profile picture to go with it. This is part of a mini project I'm working on to brush up on my Django/Python.

Thanks.

Community
  • 1
  • 1
super9
  • 29,181
  • 39
  • 119
  • 172

1 Answers1

30

Forgive me if I'm wrong, but it sounds like you don't need anything more than the default admin widget for an ImageField.

This satisfies:

  1. Uploading images using Django Admin
  2. Including a profile picture (singular) to go with a celebrity.

Also, the link you point to is pretty old. The django admin ships with javascript enabled arbitrarily long inlines these days (for at least a year I'd think), so if you want multiple images, just set up a second model that has a foreign key to your profile model. Set up an admin inline and you've got out of the box functionality!

class Celebrity(models.Model):
    name = models.CharField(max_length=100)

class Image(models.Model):
    celebrity = models.ForeignKey(Celebrity, on_delete=models.CASCADE)
    image = models.ImageField()

class InlineImage(admin.TabularInline):
    model = Image

@admin.register(models.Celebrity)
class CelebrityAdmin(admin.ModelAdmin):
    inlines = [InlineImage]
zfedoran
  • 2,986
  • 4
  • 22
  • 25
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • 1
    As i suppose that Nai expects an automatic thumbnail preview of the uploaded images some more tweaking can be done: http://stackoverflow.com/questions/1385094/django-admin-and-showing-thumbnail-images – arie Mar 29 '11 at 07:57
  • I suppose that's possible. The post he links and his language are all about uploading so it didn't cross my mind. – Yuji 'Tomita' Tomita Mar 29 '11 at 13:26