Django Error - unsupported operand type(s) for +: 'int' and 'strTypeError at

6.5k views Asked by At

I received this error an i don't know why it happend. TypeError at /admin/ofac_sdn/ofac_sdn/ unsupported operand type(s) for +: 'int' and 'strTypeError.

I have uploaded a csv directly to my models. These are the fields i used from the csv.

36,AEROCARIBBEAN AIRLINES,null ,CUBA,null,null,null,0,0,null,null,null
173,"ANGLO-CARIBBEAN CO., LTD.",null,CUBA,null,null,null,0,0,null,null,null
306,BANCO NACIONAL DE CUBA,null,CUBA,null,null,null,0,0,null,null,a.k.a. 'BNC'.

This is how my admin looks like:

from django.contrib import admin

from ofac_sdn.models import Ofac_Sdn
from ofac_sdn.models import Ofac_Add
from ofac_sdn.models import Ofac_Alt
# Register your models here.    
admin.site.register(Ofac_Sdn)    
admin.site.register(Ofac_Add)
admin.site.register(Ofac_Alt)

These are my models:

class Ofac_Sdn(models.Model):
    number = models.IntegerField(blank=True, null=True)
    name = models.CharField(max_length=200, null=True)
    b_i = models.CharField(max_length=250, null=True)
    programe= models.CharField(max_length=250, null=True)

    more_info = models.CharField(max_length=250, null=True)
    vessel_call_sign = models.CharField(max_length=250, null=True)
    vessel_type= models.CharField(max_length=250, null=True)
    vessel_dwt = models.IntegerField(blank=True, null=True)
    tonnage = models.IntegerField(blank=True, null=True)
    vessel_flag = models.CharField(max_length=250, null=True)
    vessel_owner= models.CharField(max_length=250, null=True)
    dob_aka= models.CharField(max_length=250, null=True)

These are my urls:

urlpatterns = [

    url(r'^ofac/', include('ofac_sdn.urls')),
    url(r'^admin/', admin.site.urls),

And this is what i have in my views. There is no view created, but i should see the inputs that I have in this ofac_sdn table/model.

from django.shortcuts import render
from django.http import HttpResponse


def index(request):
    return HttpResponse("<h1> Hi Cohen</h1>")

The thing is that i can see the records in my DB from an app, but they are not displayed in the admin. Thank you in advance!

Below is the full error:

TypeError at /admin/ofac_sdn/ofac_sdn/
unsupported operand type(s) for +: 'int' and 'str'
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/ofac_sdn/ofac_sdn/
Django Version: 1.11.4
Exception Type: TypeError
Exception Value:    
unsupported operand type(s) for +: 'int' and 'str'
Exception Location: /Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/models.py in __str__, line 34
Python Executable:  /Users/cohen/my-python-project/venv/bin/python
Python Version: 3.6.1
Python Path:    
['/Users/cohen/my-python-project/venv/ofac/ofac_project',
 '/Users/cohen/anaconda/lib/python36.zip',
 '/Users/cohen/anaconda/lib/python3.6',
 '/Users/cohen/anaconda/lib/python3.6/lib-dynload',
 '/Users/cohen/my-python-project/venv/lib/python3.6/site-packages']
Server time:    Tue, 5 Sep 2017 08:42:29 +0000
Error during template rendering

In template /Users/cohen/my-python-project/venv/lib/python3.6/site-packages/django/contrib/admin/templates/admin/base.html, error at line 75
unsupported operand type(s) for +: 'int' and 'str'
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 Switch to copy-and-paste view

/Users/cohen/my-python-project/venv/lib/python3.6/site-packages/django/db/models/options.py in get_field
            return self.fields_map[field_name] ...
▼ Local vars
Variable    Value
field_name  
'__str__'
self    
<Options for Ofac_Sdn>
2

There are 2 answers

0
Alfe On

you are somewhere adding a string and an int, probably a line of code like this:

return "foo" + n  # n being an int

You should replace this + by using a proper formatting, e.g.

return "foo %d" % n

or

return "foo {}".format(n)

If you really need to use +, you have to convert your int to a str first:

return "foo " + str(n)
3
Cohen On

Thank you guys! you were right! the issue was from my def. It had a '+' and somehow this cretaed the error. I could change the + sign into a comma and now it works properly.

def __str__(self):
    return self.number,  self.name,  self.programe