1

I want to create me admin like this: http://lightbird.net/dbe/_static/p1.png

What I have right now is:

def get_upload_file_name(instance, filename):
    new_file_path_and_name = os.path.join(MEDIA_ROOT,'img/albums')
    return new_file_path_and_name

class Album(models.Model):
    title = models.CharField(max_length = 60)

    def __unicode__(self):
        return self.title

class Tag(models.Model):
    tag = models.CharField(max_length = 50)

    def __unicode__(self):
        return self.tag

class Image(models.Model):
    title = models.CharField(max_length = 60, blank = True, null = True)
    image = models.FileField(upload_to = get_upload_file_name)
    tags = models.ManyToManyField(Tag, blank = True)
    albums = models.ForeignKey(Album)
    width = models.IntegerField(blank = True, null = True)
    height = models.IntegerField(blank = True, null = True)
    created = models.DateTimeField(auto_now_add=True)

    def tags_(self):
        lst = [x[1] for x in self.tags.values_list()]
        return str(join(lst, ", "))

    def albums_(self):
        lst = [x[1] for x in self.albums.values_list()]
        return str(join(lst, ", "))


    def __unicode__(self):
        return self.image.name 

class AlbumAdmin(admin.ModelAdmin):
    search_fields = ["title"]
    list_display = ["title"]

class TagAdmin(admin.ModelAdmin):
    list_display = ["tag"]

class ImageAdmin(admin.ModelAdmin):
    search_fields = ["title"]
    list_display = ["__unicode__", "title", "tags_", "albums_", "created"]
    list_filter = ["tags", "albums"]

admin.site.register(Album, AlbumAdmin)
admin.site.register(Tag, TagAdmin)
admin.site.register(Image, ImageAdmin)

I'm not sure what an acceptable input for ImageAdmin's list_display. I'm following the lightbird (http://lightbird.net/dbe/photo.html) tutorial but since it's out of date, I'm making some of my own choices along the way. I'm not sure how to modify my tags_ and albums_ method to achieve the admin layout.

I am getting the error:

File "/Users/bradfordli/Development/DjangoEnvironment/django_1_6_4/bin/PersonalWebsite/gallery/models.py" in tags_
  45.       return str(join(lst, ", "))

Exception Type: NameError at /admin/gallery/image/
Exception Value: global name 'join' is not defined

I'm not sure how to fix this as I am not sure of list_display's appropriate input

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Liondancer
  • 15,721
  • 51
  • 149
  • 255

1 Answers1

1

There is really no join() built-in function in python. join() is a method on a string.

Replace:

return str(join(lst, ", "))

with:

return ", ".join(lst)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks! HOwever I am not getting this error `'Album' object has no attribute 'values_list'` – Liondancer May 12 '14 at 02:48
  • 1
    @Liondancer I'm pretty sure you are not doing it correctly. Take a look: http://stackoverflow.com/questions/4013585/django-include-data-from-foreignkey-in-admin-list-display-function and http://stackoverflow.com/questions/163823/can-list-display-in-a-django-modeladmin-display-attributes-of-foreignkey-field. Hope it helps. – alecxe May 12 '14 at 13:48