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"
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):
return self.name
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.
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 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.
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.)
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\handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
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.