Charanjit Singh
Charanjit Singh

Reputation: 1047

TypeError in Django models, It seems like there's some problem in fields but dont know what. __str__ returned non-string (type __proxy__)

I'm facing this issue.

It seems like there's some problem in the fields. but I don't know which field. I've checked all the fields one by one, but everything is working. but when I'm opening contact, which is a wrapper around other fields, it's throwing me this error.

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/contact_management/contact/11/change/

Django Version: 2.2.2
Python Version: 3.7.5
Installed Applications:
['django.contrib.admin',
 'accounts',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework_filters',
 'AreaManagement',
 'contact_management',
 'telecaller',
 'django_extensions',
 'django_better_admin_arrayfield.apps.DjangoBetterAdminArrayfieldConfig',
 'admin_reorder',
 'rest_framework',
 'rest_framework.authtoken',
 'djoser',
 'corsheaders',
 'django_filters',
 'phonenumber_field',
 'utils',
 'control_panel',
 'background_task',
 'crm',
 'website',
 'rest_framework_swagger']
Installed Middleware:
['corsheaders.middleware.CorsMiddleware',
 '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',
 'admin_reorder.middleware.ModelAdminReorder',
 'corsheaders.middleware.CorsPostCsrfMiddleware']


Template error:
In template /media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 15
   __str__ returned non-string (type __proxy__)
   5 :     {% endif %}
   6 :     {% for line in fieldset %}
   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="fieldBox{% 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 :                             <div class="readonly">{{ field.contents }}</div>
   18 :                         {% else %}
   19 :                             {{ field.field }}
   20 :                         {% endif %}
   21 :                     {% endif %}
   22 :                     {% if field.field.help_text %}
   23 :                         <div class="help">{{ field.field.help_text|safe }}</div>
   24 :                     {% endif %}
   25 :                 </div>


Traceback:

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  145.                 response = self.process_exception_by_middleware(e, request)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  143.                 response = response.render()

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/response.py" in render
  106.             self.content = self.rendered_content

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  171.                     return self._render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  209.                     nodelist.append(node.render_annotated(context))

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/loader_tags.py" in render
  188.             return template.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  173.                 return self._render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  209.                     nodelist.append(node.render_annotated(context))

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  209.                     nodelist.append(node.render_annotated(context))

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/defaulttags.py" in render
  309.                 return nodelist.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in render
  987.             output = self.filter_expression.resolve(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in resolve
  671.                 obj = self.var.resolve(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in resolve
  796.             value = self._resolve_lookup(context)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/template/base.py" in _resolve_lookup
  858.                             current = current()

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/contrib/admin/helpers.py" in label_tag
  132.         contents = conditional_escape(self.field.label)

File "/media/charanjit/WorkStation/BNHABITAT_ENV/lib/python3.7/site-packages/django/utils/html.py" in conditional_escape
  103.         text = str(text)

Exception Type: TypeError at /admin/contact_management/contact/11/change/
Exception Value: __str__ returned non-string (type __proxy__)

Error image Django Logs

Models.py


class Contact(models.Model):
    TYPE_PERSON = 'person'
    TYPE_COMPANY = 'company'
    """ Person model is seprated from contact as person model maybe used in
        the near future for seprate applications as well
    """
    person = models.OneToOneField(
        Person, blank=True, null=True, on_delete=models.CASCADE)

    company = models.OneToOneField(
        Company, blank=True, null=True, on_delete=models.CASCADE)
    """ Potential rating is the parameter that will be used
        in the algorithmic calculation for selection of contacts for calls
    """
    potential_rating = models.IntegerField(default=0)
    """ last_contacted date will be used to set threshold for 
        when the contact is called again by same or other client/tele-caller
    """
    last_contacted = models.DateField(
        auto_now=False, auto_now_add=False, blank=True, null=True)
    """ Defines who added the contact to the system and who all have 
        access to this contact
    """
    image = models.ImageField(_(""), upload_to='contact-images/', height_field=None, width_field=None, max_length=None ,null=True)

    # def __str__(self):
    #   if self.contact_type == Contact.TYPE_PERSON:
    #       return _(str(self.person.first_name) + " " + str(self.person.last_name))
    #   if self.contact_type == Contact.TYPE_COMPANY:
    #       return _(self.company.name)
    #   return "Creating..."

    def save(self, *args, **kwargs):
        if not self.person == None and not self.company == None:
            raise AssertionError("Either of 'company' or 'person' is allowed.")
        is_safe = False
        if not self.person == None:
            is_safe = True
        if not self.company == None:
            is_safe = True
        if is_safe:
            super(Contact, self).save(*args, **kwargs)
        else:
            raise AssertionError(
                "Either of 'company' or 'person' is required.")

    @property
    def contact_type(self):
        if not self.person == None:
            return Contact.TYPE_PERSON
        if not self.company == None:
            return Contact.TYPE_COMPANY

Upvotes: 1

Views: 141

Answers (1)

shafikshaon
shafikshaon

Reputation: 6404

You need to import _ like this

from django.utils.translation import ugettext_lazy as _

Upvotes: 2

Related Questions