Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in admin: __str__ returned non-string (type NoneType)

The admin returns this error when trying to add an instance to one of my models. The model itself has a correct str() method and contains no instances yet. Also tried replacing the str() method with a static method or removing it altogether. No luck.

The error seems to point towards something going wrong in the history part of the admin. Stacktrace points to line 33.

Error during template rendering

In template /Users/snirp/juis/snirpdrive/glotto/venv/lib/python3.6/site-packages/django/contrib/admin/templates/admin/change_form.html, error at line 33
__str__ returned non-string (type NoneType)
23  {% endblock %}
24  {% endif %}
25  
26  {% block content %}<div id="content-main">
27  {% block object-tools %}
28  {% if change %}{% if not is_popup %}
29    <ul class="object-tools">
30      {% block object-tools-items %}
31      <li>
32          {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
33          <a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
34      </li>
35      {% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif %}
36      {% endblock %}
37    </ul>
38  {% endif %}{% endif %}
39  {% endblock %}
40  <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" novalidate>{% csrf_token %}{% block form_top %}{% endblock %}
41  <div>
42  {% if is_popup %}<input type="hidden" name="{{ is_popup_var }}" value="1" />{% endif %}
43  {% if to_field %}<input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}" />{% endif %}

These are the relevant parts of my models.py and admin.py

class UserContent(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    created_by = models.ForeignKey(User, related_name='%(class)s_creator')
    updated_by = models.ForeignKey(User, related_name='%(class)s_updater')

    class Meta:
        abstract = True


class Linetrans(UserContent):
    line = models.ForeignKey(Line)
    translation = models.ForeignKey(Translation)
    text = models.CharField(max_length=400)

    #def __str__(self):
    #    return self.text

    class Meta:
        ordering = ['line']

and

admin.site.register(Linetrans)

Other model classes are very similar and do not return an error. The error also occurs when the Linetrans is added as an inline to another admin class.

edit / update: I commented out all other str() methods in my model and sure enough the error seems to go away. Now trying to pinpoint the issue.

like image 971
Roy Prins Avatar asked Feb 14 '17 15:02

Roy Prins


5 Answers

Turns out that there was an unexpected empty CharField in a related model. Leaving this an an answer, because it might help others.

Troubleshoot the issue by systematically commenting out the __str__() methods of your models until you find the offending model. Work from there to identify the offending record(s).

like image 126
Roy Prins Avatar answered Nov 11 '22 12:11

Roy Prins


I had a similar issue. The problem was that the primary key for one row was null ( I dont know how that happened). I couldnt delete the row because of cascade issues. so I had to change the str mmethod to somthing like this.

def __str__(self):
    if self.customerName==None:
        return "ERROR-CUSTOMER NAME IS NULL"
    return self.customerName
like image 26
Dhanushka Amarakoon Avatar answered Nov 11 '22 13:11

Dhanushka Amarakoon


Check out the class that you have referred to in

        models.ForeignKey 

The problem lies in the

        __str__() 

method of one of those classes. Then, add

                             or ''

in the return statement of that class like this.

        def __str__(self):
            return self.title or ''
like image 10
Dimanjan Avatar answered Nov 11 '22 13:11

Dimanjan


def __str__(self):
        return str(self.topic)

Use str to make it type string. Then it's not not-string type.

like image 6
1UC1F3R616 Avatar answered Nov 11 '22 11:11

1UC1F3R616


For this error you can use two solutions

first solution: you can comment it out below code

 def __str__(self):
    return self.id

to

 #def __str__(self):
 #  return self.id

secound solution: you can return the string from the object

 def __str__(self):
    return str(self.id)
like image 3
sanjay Avatar answered Nov 11 '22 12:11

sanjay