Liondancer
Liondancer

Reputation: 16479

Admin interface for Gallery App

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

Upvotes: 1

Views: 50

Answers (1)

alecxe
alecxe

Reputation: 474191

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)

Upvotes: 1

Related Questions