递归错误 - 当外键('self') django 时超过最大递归部门



我在试图打开模型时会在管理员控制台中遇到错误(在我的情况下)。这是在添加一个外国基金("自我")的字段之后发生的。我想这与 str 方法相抵触。如果我评论该方法,则没有错误,但是模型中的所有对象都以"员工对象"的形式出现。这是我的错误看起来像https://ibb.co/jht84q

这是我的型号:

from django.db import models
import calendar  
from datetime import datetime
from datetime import timedelta
class employees(models.Model):
    emp_id=models.PositiveIntegerField()
    emp_name = models.CharField(max_length = 100)
    emp_lname = models.CharField(max_length = 100)
    emp_loc = models.CharField(max_length = 100,null=True)
    manager_id=models.ForeignKey('self',null=True,blank=True)
    image=models.ImageField(upload_to='profile_image',default='/profile_image/profile-icon.png')
    email = models.EmailField(default='app-engine@gmail.com', blank=False)
    def __str__(self):
        return str(self.emp_id) + '-' + self.emp_name + '-' + self.emp_loc+'-'+str(self.manager_id)

class leave(models.Model): 
    employee = models.ForeignKey(employees, on_delete=models.CASCADE, default='1')
    start_date = models.DateField()
    end_date = models.DateField()
    status=models.CharField(max_length=1,default='P')
    ltype=models.CharField(max_length=2)
    message=models.CharField(max_length=500,blank=True)
    date_created = models.DateTimeField(auto_now_add=True)
    def leave_length(self):
          return self.end_date - self.start_date+timedelta(days=1);
    def __str__(self):
        return str(self.id) + '/' + str(self.employee.emp_name) +'/'+str(self.start_date) +'/'+str(self.end_date) +'/'+str(self.status)+'/'+str(self.date_created)

这是我的追溯:

Environment:

Request Method: GET
Request URL: http://localhost:8000/admin/apply/employees/
Django Version: 1.11.3
Python Version: 3.6.0
Installed Applications:
['apply.apps.ApplyConfig',
 'm_manage',
 'pending.apps.PendingConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
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']

Template error:
In template C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocontribadmintemplatesadminbase.html, error at line 75
   maximum recursion depth exceeded while calling a Python object   65 :           <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
   66 :         {% endfor %}</ul>
   67 :         {% endif %}
   68 :     {% endblock messages %}
   69 : 
   70 :     <!-- Content -->
   71 :     <div id="content" class="{% block coltype %}colM{% endblock %}">
   72 :         {% block pretitle %}{% endblock %}
   73 :         {% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
   74 :         {% block content %}
   75 :          {% block object -tools %}{% endblock %}
   76 :         {{ content }}
   77 :         {% endblock %}
   78 :         {% block sidebar %}{% endblock %}
   79 :         <br class="clear" />
   80 :     </div>
   81 :     <!-- END Content -->
   82 : 
   83 :     {% block footer %}<div id="footer"></div>{% endblock %}
   84 : </div>
   85 : <!-- END Container -->

Traceback:
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangodbmodelsoptions.py" in get_field
  617.             return self.fields_map[field_name]
During handling of the above exception ('__str__'), another exception occurred:
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocontribadminutils.py" in lookup_field
  283.         f = _get_non_gfk_field(opts, name)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocontribadminutils.py" in _get_non_gfk_field
  317.     field = opts.get_field(name)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangodbmodelsoptions.py" in get_field
  619.             raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
During handling of the above exception (employees has no field named '__str__'), another exception occurred:
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangodbmodelsfieldsrelated_descriptors.py" in __get__
  178.             rel_obj = getattr(instance, self.cache_name)
During handling of the above exception ('employees' object has no attribute '_manager_id_cache'), another exception occurred:
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocorehandlersexception.py" in inner
  41.             response = get_response(request)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocorehandlersbase.py" in _get_response
  217.                 response = self.process_exception_by_middleware(e, request)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocorehandlersbase.py" in _get_response
  215.                 response = response.render()
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplateresponse.py" in render
  107.             self.content = self.rendered_content
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplateresponse.py" in rendered_content
  84.         content = template.render(context, self._request)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebackendsdjango.py" in render
  66.             return self.template.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render
  207.                     return self._render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in _render
  199.         return self.nodelist.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render
  990.                 bit = node.render_annotated(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render_annotated
  957.             return self.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplateloader_tags.py" in render
  177.             return compiled_parent._render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in _render
  199.         return self.nodelist.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render
  990.                 bit = node.render_annotated(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render_annotated
  957.             return self.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplateloader_tags.py" in render
  177.             return compiled_parent._render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in _render
  199.         return self.nodelist.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render
  990.                 bit = node.render_annotated(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render_annotated
  957.             return self.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplateloader_tags.py" in render
  72.                 result = block.nodelist.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render
  990.                 bit = node.render_annotated(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render_annotated
  957.             return self.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplateloader_tags.py" in render
  72.                 result = block.nodelist.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render
  990.                 bit = node.render_annotated(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatebase.py" in render_annotated
  957.             return self.render(context)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangotemplatelibrary.py" in render
  225.         _dict = self.func(*resolved_args, **resolved_kwargs)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocontribadmintemplatetagsadmin_list.py" in result_list
  340.             'results': list(results(cl))}
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocontribadmintemplatetagsadmin_list.py" in results
  316.             yield ResultList(None, items_for_result(cl, res, None))
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocontribadmintemplatetagsadmin_list.py" in __init__
  307.         super(ResultList, self).__init__(*items)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocontribadmintemplatetagsadmin_list.py" in items_for_result
  218.             f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "C:UsersrhshankaAppDataLocalProgramsPythonPython36libsite-packagesdjango-1.11.3-py3.6.eggdjangocontribadminutils.py" in lookup_field
  299.                 value = attr()
File "C:UsersrhshankaLMSapplymodels.py" in __str__
  15.         return str(self.emp_id) + '-' + self.emp_name + '-' + self.emp_loc+'-'+str(self.manager_id)

Exception Type: RecursionError at /admin/apply/employees/
Exception Value: maximum recursion depth exceeded while calling a Python object

尝试:

def __str__(self):
    self_str = '%s-%s-%s' % (self.emp_id, self.emp_name, self.emp_loc)
    if self.manager_id and self.manager_id.pk != self.pk:
        return '%s-%s' % (self_str, str(self.manager_id))
    return self_str

您需要确保您的逻辑正确,对于许多继承,它将无法读取。

最新更新