dobleV
dobleV

Reputation: 307

Drop-down box in Django admin does not auto-populate with choices

I have 2 models in my models.py file; Genre and AudioTracks. Each track will be assigned a genre; as such, I have defined a ForeignKey relationship. My problem is that in the admin view, when i want to add the genre fore each track, I can't select the choices from the combo box. I have to click on the add symbol (i.e. the green +) and then a Genre window pops up and I can then add the genre.

I want to be able to just add a track and select the genre in the little combo box at the bottom.

Here are the images:

I want to be able to see the genre choices instead of clicking "add"

Second Image:

What happens when I click "add" A snippet of my code is below:

GENRE_CHOICES =  (
             ('rock', 'Rock'),
             ('jazz/blues', 'Jazz/Blues'),
             ('blues', 'Blues'),
             ('r&b', 'R&B'),
             ('jazz', 'Jazz'),
             ('pop', 'Pop'), 
             ('hip-hop', 'Hip-Hop'),    
             )


def get_upload_path(dirname, obj, filename):
  return os.path.join("audiotracks", dirname, obj.user.username, filename)

def get_audio_upload_path(obj, filename):
  return get_upload_path("audio_files", obj, filename)


class Genre(models.Model):
    genre_choices = models.CharField(max_length=1, choices=GENRE_CHOICES)
    slug = models.SlugField(max_length = 100, unique = True) 

def __unicode__(self):
    return self.title

def get_absolute_url(self):
    return ('view_midmentals_genre', None, {'slug':self.slug})

class AudioTrack(models.Model):
  class Meta:
      pass

user = models.ForeignKey(User, 
    related_name = "tracks",
    blank = True,
    null = True
)

added_on = models.DateTimeField(auto_now_add=True, null = True)
updated_on = models.DateTimeField(auto_now=True, null = True)
audio_file = models.FileField(_("Audio file"), upload_to=get_audio_upload_path)
title = models.CharField(_("Title"), max_length="200", null=True)
description = models.TextField(_("Description"), null=True, blank=True)
slug = models.SlugField(max_length = 40, unique = True) #so as to have a dedicated page for each category 
genre = models.ForeignKey(Genre)

Please let me know if i need to rephrase my question or title. I was having a hard time putting it into words. Thank you very much.

Upvotes: 1

Views: 1146

Answers (1)

YardenST
YardenST

Reputation: 5256

You have the + icon because probably you have not added any records to the Genre Model. Once you add one, the combo box will contain this record.

But it seems the you already knows what the Genres are going to be so you can just do this:

class AudioTrack(models.Model):
    genre = models.CharField(..., choices = GENRE_CHOICES)

and the combo box will contain the choices you have in the list.

and consider do you choices list like:

GENRE_CHOICES = ((0,'Rock'),(1, 'Jazz'),(2,'Hip Hop'))

and after:

genre = models.PositiveSmallIntegerField(choices = GENRE_CHOICES)

Upvotes: 2

Related Questions