paradox
paradox

Reputation: 258

Django unable to detect the existance of a model's field

here is my model:

class Question(models.Model):
    id = models.AutoField(primary_key=True)
    question_title = models.CharField(max_length=100, default="no title")
    question_text = models.CharField(max_length=200, default ="no content")

    def __unicode__(self):
        return self.question_title + self.question_text

and in my admin configuration page, I setup my QuestionAdmin class like this:

class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Question ID', {'fields':['id_pk']}),
        ('Question content',{'fields':['question_title',"question_text"]})
    ]

and then I applied this configuration into django's admin page:

admin.site.register(Question, QuestionAdmin)

and here is my full error trace:

Environment:


Request Method: GET
Request URL: http://www.whiletrue.cc/paradox/admin/polls/question/add/

Django Version: 1.11.2
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'polls.apps.PollsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  551.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  224.             return view(request, *args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
  1508.         return self.changeform_view(request, None, form_url, extra_context)

File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1408.             return self._changeform_view(request, object_id, form_url, extra_context)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in _changeform_view
  1437.         ModelForm = self.get_form(request, obj)

File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in get_form
  651.                 % (e, self.__class__.__name__)

Exception Type: FieldError at /paradox/admin/polls/question/add/
Exception Value: Unknown field(s) (id) specified for Question. Check fields/fieldsets/exclude attributes of class QuestionAdmin.

I've already made my backend sqlite database updated using manage.py makemigrations and migrate command, so in my database table polls_question, there should be a primary key named id in the shcema.

Upvotes: 1

Views: 65

Answers (1)

Cory Madden
Cory Madden

Reputation: 5193

The problem is that the id field cannot be changed and must be rendered as a read only field. Here is an example of how to do it:

class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Question ID', {'fields':['id']}),
        ('Question content',{'fields':['question_title',"question_text"]})
    ]
    readonly_fields = ('id', )

You have to define it in the readonly_fields property of the ModelAdmin class, which must be a list or tuple.

Upvotes: 1

Related Questions