Error when using the User Model in Django.

I have created this affiliate model:


class Affiliate(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    tracking_code = ShortUUIDField(unique=True, length=100, max_length=200, alphabet="abcdefghijklmnopqrstuvxyz1234567890")
    product = models.ManyToManyField(store_model.Product)
    commission = models.CharField(max_length=5000, null=True, blank=True)
    earning = models.CharField(max_length=5000, null=True, blank=True)
    status = models.CharField(max_length=30, choices=ACTIVE_STATUS, default="Active")
    date= models.DateField(auto_now_add=True)

    def __str__(self):
        return self.user

With the user field, i get this error:

TypeError at /admin/affiliate/affiliate/add/
__str__ returned non-string (type NoneType)

This error occurs when i am trying to add an object from the Django admin panel.

But without the user field, everything works fine, but I need the user field to be included in the model.
What could be going wrong here?

This is the full traceback

System check identified no issues (0 silenced).
July 20, 2023 - 10:04:58
Django version 3.2.18, using settings 'ecommerce_prj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /admin/affiliate/affiliate/add/
Traceback (most recent call last):
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\core\handlers\exception.py", line 47, in inner   
    response = get_response(request)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\response.py", line 105, in render       
    self.content = self.rendered_content
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\backends\django.py", line 61, in render 
    return self.template.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\loader_tags.py", line 150, in render    
    return compiled_parent._render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\loader_tags.py", line 150, in render    
    return compiled_parent._render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\loader_tags.py", line 62, in render     
    result = block.nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\loader_tags.py", line 62, in render     
    result = block.nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\loader_tags.py", line 62, in render     
    result = block.nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\loader_tags.py", line 195, in render    
    return template.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 172, in render
    return self._render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\defaulttags.py", line 214, in render    
    nodelist.append(node.render_annotated(context))
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\defaulttags.py", line 315, in render    
    return nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\loader_tags.py", line 195, in render    
    return template.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 172, in render
    return self._render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\defaulttags.py", line 214, in render    
    nodelist.append(node.render_annotated(context))
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\defaulttags.py", line 214, in render    
    nodelist.append(node.render_annotated(context))
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\defaulttags.py", line 315, in render    
    return nodelist.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 905, in render_annotated 
    return self.render(context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 994, in render
    return render_value_in_context(output, context)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\template\base.py", line 973, in render_value_in_context
    value = str(value)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\utils\html.py", line 376, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\boundfield.py", line 34, in __str__        
    return self.as_widget()
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\boundfield.py", line 97, in as_widget      
    renderer=self.form.renderer,
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\widgets.py", line 246, in render
    context = self.get_context(name, value, attrs)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\contrib\admin\widgets.py", line 283, in get_context
    'rendered_widget': self.widget.render(name, value, attrs),
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\widgets.py", line 246, in render
    context = self.get_context(name, value, attrs)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\widgets.py", line 683, in get_context      
    context = super().get_context(name, value, attrs)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\widgets.py", line 644, in get_context      
    context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\widgets.py", line 592, in optgroups        
    for index, (option_value, option_label) in enumerate(self.choices):
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\models.py", line 1170, in __iter__
    yield self.choice(obj)
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\models.py", line 1184, in choice
    self.field.label_from_instance(obj),
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\forms\models.py", line 1253, in label_from_instance
    return str(obj)
TypeError: __str__ returned non-string (type NoneType)
[20/Jul/2023 10:05:03] "GET /admin/affiliate/affiliate/add/ HTTP/1.1" 500 717083
Exception ignored in: <generator object cursor_iter at 0x00000164A46FAEC8>
Traceback (most recent call last):
  File "C:\Users\Destiny Franks\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\django\db\models\sql\compiler.py", line 1649, in cursor_iter
    cursor.close()
sqlite3.ProgrammingError: Cannot operate on a closed database.

What does your ModelAdmin class look like for this model?

Looking like this:

class AffiliateAdmin(ImportExportModelAdmin):
	list_filter = ('status',)
	list_display = ('tracking_code', 'status', 'date')

Are you using the standard Django User model or a custom User?

If you’re using a custom User, what does its __str__ function look like?

What does this ImportExportModelAdmin class look like?

Note: I am unable to recreate this issue based upon the information posted so far.

I was checking the users that have been created in the database from the admin section, and I found some users without usernames, and since this is how the __str__ function looks like:

def __str__(self):
        return self.username

I deleted the users without usernames, and the error was gone.
I also updated the __str__ to this:

    def __str__(self):
        if self.username:
             return self.username
        else:
            return f"No Username"

Is the new __str__ correct?

The new __str__ will no longer cause that error.

However, I’m not sure how/why you would ever have users in your database without a username. (You could also use an alternate field to get a value so you don’t have multiple entries of “No Username” in the drop-down list.)

1 Like

Okay, I did it using the ID field instead.