An issue changing the object display name of a model (forein key linked to a 'many-to-many' model)

I’m trying to link two models, and I want the object display name on one model to come from the other one.

class Customer(models.Model):

    company_name = models.ForeignKey(ClientModel, null=True, on_delete=models.PROTECT) 
    description = models.TextField(default='', blank=True)
    PersonResponsible = models.ForeignKey(PersonResponsible, null=True, on_delete=models.PROTECT, verbose_name="Person/Company responsible") 

    def __str__(self):
        return str(self.company_name)  # this will not display the company name I defined in "ClientModel", instead I get "None"

Visually this model looks like this:

The “company name” field links to this many-to-many model called “ClientModel”:

class ClientModel(models.Model):
    name = models.CharField(max_length=200)
    id = models.UUIDField(default=uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):

I add the customers in the ClientModel:


On the objects for Customer model, I want it to show the name of the customers defined in Client model, but I just get “None”:


Instead of “None”, I want it to display “Customer001” which is the object name returned from ClientModel. I’m not sure if this is possible, or if there’s a way.

Please post your Admin classes for these models.

as for what’s relevant in my admin, I just have this:

from .models import (


Initially I define the manyTomany relationship for ClientModel in the main model called “DashboardModel”

 client = models.ManyToManyField('ClientModel',  blank=True)

I tried changing this:

company_name = models.ForeignKey(ClientModel, null=True, on_delete=models.PROTECT) 

to this:

 company_name = models.ManyToManyField(ClientModel, blank=True) 

but then I get this:


(“dashboard” is the name of my app btw)

Working from what you originally posted - with the FK from Customer to ClientModel, I do not get the behavior you describe.

That’s interesting, and with the same code used for def __str__?


The only difference between my test models and your code in the original post at the top is that I removed the PersonResponsible field because you did not post a PersonResponsible model - but I think we can both agree that that should not affect this.

What versions of Django and Python are you using? Have you implemented any custom JS or CSS for the admin page?

I’m using Django 4.1 and Python 3.9.13 on Windows

I was working on this project with someone else (who now left) and he was overriding the admin page, but there was no css or JS added to it as far as I know.

would this also work for you if you do -

 def __str__(self):
        return self.company_name

instead of:

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

I’m curious

Yes, it still works. (Note: My test site is still using Django 3.2 - I’m setting up a Django 4.1 version to see if there are any version-specific issues.)

Interesting, if I don’t cast it to string (which I also shouldn’t have to) I am getting this:

TypeError: __str__ returned non-string (type NoneType)

the project is still in development right now so I might try and flush the database and see if it helps, thanks for taking interest

That really implies to me that you don’t have the right foreign key value in the database - something isn’t matching up. I’d examine the database directly to ensure the keys are all what you expect them to be.

I removed any association of that ClientModel from the original Dashboard model, but I still experience the same situation, it starts by throwing off Python 3.9 errors

Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\wsgiref\", line 137, in run
    self.result = application(self.environ, self.start_response)

It’s very strange.

A follow-up - changing to 4.1 does create an issue with the “non str wrapper” version, but still produces correct results with the str(self.company_name).

But, the error I get is TypeError: __str__ returned non-string (type ClientModel) - which is different than the NoneType error you’re reporting, leading me to believe there’s an FK issue involved.

It is working now.
I think what was affecting the foreign key was the UUIDField defined in the “ClientModel” model.
When I removed it and reset the DB, the problem wasn’t there.

Funnily enough I landed on the same:
TypeError: __str__ returned non-string (type ClientModel) -

So I used the string wrapper to get past this.
Strange stuff, I think the issue is solved… I had nearly given up! thanks for the help in resolving the problem.