koekiezorro
koekiezorro

Reputation: 115

ForeignKey uneditable in Django Admin

Something that has worked perfectyle until recently has stopped working. A model that has a foreignkey to another object isn't editable as expected in the standard django admin. The problem is this: ForeignKeys to some model aren't editable in de admin interface, though they should be, although a label does appear in the admin page. It looks like this: Django interface edit-page for the question-model

The HTML-code for the bit where the foreignkey to the Story-model should be edited:

<div class="form-row field-story">
        <div>
            <label for="id_story" class="required">Story:</label>           
        </div>
</div>

2 relevant models with their respective unicode defs:

class Question(models.Model):
    question = models.CharField(max_length = 200)#the Actual question
    correct_answer = models.ForeignKey(Answer, verbose_name = 'Correct Answer', related_name = 'Correct answer')
    incorrect_answers = models.ManyToManyField(Answer, verbose_name = 'Distractor options', related_name = 'Incorrect answers')
    story = models.ForeignKey(Story)

    def __unicode__(self):
        try:
             return self.question.decode('latin-1') + '(%i)'%(self.id)
        except:
            return str(self.id)


class Story(models.Model):
    class Meta:
        verbose_name_plural = 'Stories'

    author = models.ForeignKey(User, blank = True, null = True, on_delete = models.SET_NULL, editable = False)
    name = models.CharField(max_length = 150, verbose_name = 'Name/Summary')
    story = models.TextField(verbose_name = 'Story', max_length = 13 * 54)#the actual story
    publish = models.BooleanField(default = False, help_text = 'If this is published, it will be uneditable.')
    date_added = models.DateTimeField (auto_now_add = True, editable = False)#date of reply
    ready = models.BooleanField(default = False, help_text = 'Whether the author thinks this is ready')
    erf = models.CharField(max_length = 20, verbose_name = 'ERF label', editable = False, blank  = True, null = True)

    def __unicode__(self):
        try:
            return "'"+self.name.encode( 'latin-1') + "'"
        except:
            return "Story with unicode name or something: %i" %(self.id)

In admin.py:

admin.site.register(Question, )

Looking at what works and what doesn't, I'm beginning to feel it has got something to do with the Story-model. Other foreignkey relationships are functioning fine. Of course, the fact that the foreignkey isn't editable means the object can't be saved from the admin, even though MySQL shows that there is a valid story_id in the question table.

I remember having used this and that this worked fine. I suspect unicode-problems somewhere, but I can't imagine what exactly, let alone how to fix them. Can anybody help?

PS. What happens at ForeignKey field will not appear in Django admin site isn't the case here, I think.

Upvotes: 1

Views: 852

Answers (1)

koekiezorro
koekiezorro

Reputation: 115

Urgh.. looking for other foreignfield problems, I encountered ForeignKey field problem in Django. In the comments to the question, it is stated by Daniel Roseman that the unicode defs on models should return unicode. Tried that, and my problem was solved.

Grrr.. Unicode... You win this time!

Upvotes: 1

Related Questions