1

I have a Django blog with a "Post" model like so:

class Post(models.Model):
    title = models.CharField(max_length=1000)
    author = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    text = models.TextField(max_length=10000)
    CATEGORY_CHOICES = (
         ('breakfast','Breakfast'),
         ('brunch','Brunch'),
         ...
    )
    category = models.CharField(max_length=100,blank=True,choices=CATEGORY_CHOICES)

I have uploaded a bunch of "Post" objects (with all of the above fields completed) to my database using the python manage.py loaddata fixturename.json command. However, when I log into the admin site to modify the objects, the values I uploaded for the "category" field do not show up as pre-selected in the field's drop-down list. Why is this? And is there any way to fix it?

EDIT: Here is snippet from my json fixture file:

    {
    "pk": 34, 
    "model": "blogs.post", 
    "fields": {
        "category": "breakfast",  
        "author": "jennaboller", 
        "text": "I have to say, my favorite weekend breakfast is blah blah blah", 
        "title": "Corned Beef Hash & Eggs: My Favorite Weekend Breakfast", 
        "pub_date": "2011-01-25 05:05:37"
    }
GChorn
  • 1,267
  • 1
  • 19
  • 36
  • Are you sure the data in the category field corresponds to the raw values, ie the first element in each CHOICES tuple? What do you see when you get a Post instance in the shell and do `print post.category`? – Daniel Roseman Sep 19 '12 at 08:04
  • @DanielRoseman, if I use `print` I get `breakfast`. If I just use `post.category` I get `u'breakfast'`. But I've already tried changing the CHOICES tupple to unicode values and it didn't help. – GChorn Sep 19 '12 at 08:31
  • Can you show us an excerpt, i.e. a single `Post` object instance from your `fixturename.json`? Otherwise, I'm afraid it's difficult to help you. However, your issue might be related to the attribute `blank=True` in the `category` field. Also, what does your `PostAdmin` admin config class look like? – pemistahl Sep 19 '12 at 10:28
  • @PeterStahl, I edited my post to show a piece of my JSON file. My PostAdmin config class subclasses `admin.ModelAdmin` and is just two lines, `inlines=[PostImageInline]` and `Media=CommonMedia`. The first references a class that subclasses `admin.TabularInline` and the other `CommonMedia` is just a class with a js file tuple and a css file dictionary. – GChorn Sep 19 '12 at 12:20
  • 1
    Mhm...nothing peculiar about your JSON data. Can you remove the attribute `blank=True` from the `category` field and test it again? This is my only idea for now, unfortunately... – pemistahl Sep 19 '12 at 12:32
  • @PeterStahl, I think this might have been the solution--I removed the `blank=True` attribute and the `category` field is now pre-selected! Do you want to post an actual answer so I can give you the green checkmark? – GChorn Sep 20 '12 at 03:49

1 Answers1

1

According to what I said in my last comment to the question, this issue is most probably related to the attribute blank=True in the category field of the Post model. But since you specified a particular category for your example model instance (breakfast), it should show up in the admin configuration as well.

The reason for this behaviour might be that Django doesn't expect the blank attribute in a CharField that has set the choices attribute because it doesn't really make sense here. If you want to be able to say that a Post instance doesn't belong to a particular category, then it's probably better to include another option in the CATEGORY_CHOICES such as ('none', 'None'). Maybe this could also be a bug in Django's admin module, but I don't know about this.

pemistahl
  • 9,304
  • 8
  • 45
  • 75