Environmental Science ：
I am here Django Of
admin.py In the file for a Models Configured with
fields Field , Cause to view a specific data , Tips ：
Django "Key 'id' not found in 'xxx'. Choices are: xxx"
Cause error code ：
@admin.register(Category) class CategoryAdmin(admin.ModelAdmin): list_display = ['id','name'] fields = ['id','name'] # This line of code causes an error
id Auto increment of settings ID key , In the database, the corresponding is
AUTO_INCREMENT, So this field is not allowed to be edited , and
fields The configuration setting is to display the edit fields , This leads to conflict , That's why it's wrong .
terms of settlement ：
Just change the code to the following
@admin.register(Category) class CategoryAdmin(admin.ModelAdmin): list_display = ['id','name'] fields = ['name'] # Remove this line of code id Field
This article was first published in BigYoung Way station