开发者

Add other object and receive error MultiValueDictKeyError (Django admin)

开发者 https://www.devze.com 2023-04-07 14:59 出处:网络
I have a relationship follow as: class Question(models.Model): qid = models.PositiveIntegerField(primary_key=True)

I have a relationship follow as:

class Question(models.Model):
   qid = models.PositiveIntegerField(primary_key=True)
   content = models.CharField(max_length=128)

class Answer(models.Model):
   answerid = models.PositiveIntegerField(primary_key=True)
   content = models.CharField(max_length=128)
   question = models.ForeignKey(Question)


class AnswerInline(admin.StackedInline):
   model = Answer
   readonly_fields = ('answerid',)
   extra = 0

class QuestionAd开发者_开发知识库min(admin.ModelAdmin):    
    inlines = [AnswerInline]
    exclude = ('id')
    list_display = ('content',)

admin.site.register(Question,QuestionAdmin)

Suppose I have a question namely A and I haven't any answer for this question yet. So, when I add an answer of A. It's ok. However, I try to add an other answer, system output an error MultiValueDictKeyError:

"Key 'oam_answer_set-0-answerid' not found in QueryDict:...

Because both of 'qid' and 'answerid' are not an AutoField. So, when I save an object, django admin can not insert a new row into database (missing primary key).

The AutoField is declared an IntegerField. However, I would like the field type of primary key is PositiveIntegerField. For that reason, how can I customize an AutoField?

Thanks


Try registering the Answer model with the admin i.e.

...
admin.site.register(Answer)

at the bottom of the admin.py. You are using fields on the inline (specifically readonly_fields) that are inherited from ModelAdmin, but you might not have registered that model.

0

精彩评论

暂无评论...
验证码 换一张
取 消