/ admin / myapp / userprofile / 1 /中的KeyError

时间:2015-03-28 01:30:54

标签: python django django-1.7

我将Django从1.6升级到1.7。

现在我在“管理”面板中收到此错误:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/myapp/userprofile/1/

Django Version: 1.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.sites',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'myapp',
 'feincms',
 'rest_framework',
 'subdomains',
 'social.apps.django_app.default')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'subdomains.middleware.SubdomainURLRoutingMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
 'myapp.middleware.SocialAuthExceptionMiddleware')


Template error:
In template /home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 17
   manager
   7 :         <div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">


   8 :             {% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}


   9 :             {% for field in line %}


   10 :                 <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>


   11 :                     {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}


   12 :                     {% if field.is_checkbox %}


   13 :                         {{ field.field }}{{ field.label_tag }}


   14 :                     {% else %}


   15 :                         {{ field.label_tag }}


   16 :                         {% if field.is_readonly %}


   17 :                             <p> {{ field.contents }} </p>


   18 :                         {% else %}


   19 :                             {{ field.field }}


   20 :                         {% endif %}


   21 :                     {% endif %}


   22 :                     {% if field.field.help_text %}


   23 :                         <p class="help">{{ field.field.help_text|safe }}</p>


   24 :                     {% endif %}


   25 :                 </div>


   26 :             {% endfor %}


   27 :         </div>


Traceback:
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  137.                 response = response.render()
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/response.py" in render
  103.             self.content = self.rendered_content
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  80.         content = template.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  148.             return self._render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  201.                             nodelist.append(node.render(context))
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  150.                 return template.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  148.             return self._render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  201.                             nodelist.append(node.render(context))
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  201.                             nodelist.append(node.render(context))
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  312.                 return nodelist.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  312.                 return nodelist.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/debug.py" in render
  90.             output = self.filter_expression.resolve(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in resolve
  596.                 obj = self.var.resolve(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in resolve
  734.             value = self._resolve_lookup(context)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/template/base.py" in _resolve_lookup
  788.                             current = current()
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/contrib/admin/helpers.py" in contents
  183.             f, attr, value = lookup_field(field, obj, model_admin)
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/contrib/admin/utils.py" in lookup_field
  280.                 value = attr()
File "/home/ashish/.virtualenvs/myapp_venv/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in __call__
  654.             manager = getattr(self.model, kwargs.pop('manager'))

Exception Type: KeyError at /admin/myapp/userprofile/1/
Exception Value: u'manager'

我应该在代码中做出哪些更改,以便我可以使用Django 1.7?

我是否必须在某些相关领域进行更改?

编辑:

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator

# Create your models here.

GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )



class UserProfile(AbstractUser):
        dob=models.DateTimeField(blank=True,null=True)
        gender = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True,blank=True)
        phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
        phone_number = models.CharField(validators=[phone_regex], blank=True,max_length=16,null=True) # validators should be a list 
        location=models.CharField(max_length=30,null=True,blank=True)
        about=models.CharField(max_length=140,null=True,blank=True)

admin.py

from django.contrib import admin
# Register your models here.
from myapp.models import UserProfile

class user_index_Admin(admin.ModelAdmin):
    def get_readonly_fields(self, request, obj=None):
        if obj:
            return UserProfile._meta.get_all_field_names()
        else:
            return []
    def has_delete_permission(self, request, obj=None):
        return False
admin.site.register(UserProfile,user_index_Admin);

1 个答案:

答案 0 :(得分:3)

错误在此行的管理类中: return UserProfile._meta.get_all_field_names()。我想把所有的字段都只读。我根据此SO answer更改了代码,但它确实有效。

admin.py

from django.contrib import admin

# Register your models here.
from myapp.models import UserProfile

class user_index_Admin(admin.ModelAdmin):
    def get_readonly_fields(self, request, obj=None):
        if obj:
            return [f.name for f in self.model._meta.fields]
        else:
            return []
    def has_delete_permission(self, request, obj=None):
        return False
admin.site.register(UserProfile,user_index_Admin);