85๐
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).
16๐
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
- [Django]-Django 1.10.1 'my_templatetag' is not a registered tag library. Must be one of:
- [Django]-How to validate a field on update in DRF?
- [Django]-Invalid requirements.txt on deploying django app to aws beanstalk
10๐
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 ''
- [Django]-Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT
- [Django]-Django Rest Framework โ Updating a foreign key
- [Django]-Django Facebook Connect App Recommendation
6๐
def __str__(self):
return str(self.topic)
Use str to make it type string.
Then itโs not not-string type.
- [Django]-How do I display the Django '__all__' form errors in the template?
- [Django]-Django models filter by foreignkey
- [Django]-Displaying a Table in Django from Database
3๐
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)
- [Django]-How to format time in django-rest-framework's serializer?
- [Django]-How do I access the child classes of an object in django without knowing the name of the child class?
- [Django]-How can I add a button into django admin change list view page
2๐
thatโs works for me:
def __str__(self):
return f"{self.category}"
Just use "f" string
- [Django]-How to show a message to a django admin after saving a model?
- [Django]-How to do SELECT MAX in Django?
- [Django]-Read-Only Field in Django Form
1๐
You can also delete the instances in the model causing this problem from the django shell python manage.py shell
. This helps when you are dealing with a model with lots of related models like mine.
Typically this problem is caused by altering model fields and
migrating, such that the instances are incompatible with each other
and related fields
- [Django]-Django Passing Custom Form Parameters to Formset
- [Django]-Django 3.2 exception: django.core.exceptions.ImproperlyConfigured
- [Django]-The view didn't return an HttpResponse object. It returned None instead
1๐
If you are trying to use __str__()
to return the value of a ForeignKey
or a OneToOneField
this would return error
Example:
def __str__():
return self.user # where user is a key to other model class
It should be:
def __str__():
return self.user.__str__()
# Or basically any other way that would explicitly unbox the value inside the related model class.
- [Django]-How to use custom manager with related objects?
- [Django]-How to add Indian Standard Time (IST) in Django?
- [Django]-Django template includes slow?
1๐
In my case I had the same issue and its origin was that: for the field used in the __str__
functions, has null values saved in the data base.
So I can solve the issue doing somethings like this:
update table
set field_for_STR_use = 'any value'
where field_for_STR_use is null or field_for_STR_use = ''
after that, I only refreshed my view and the problem had disappeared.
- [Django]-Using django-admin on windows powershell
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
- [Django]-DRF: custom ordering on related serializers
1๐
There was one of the foreign reference which was returning non string field. I changed it to string field or you can change to str(foreign-key-return-element)
name = models.CharField(max_length=200, unique=True) # name of the article
parent = models.ForeignKey(โselfโ,on_delete=models.DO_NOTHING,related_name=โcategory_parent_itemโ, default=None, null=True) # Store child items
created_at = models.DateTimeField(auto_now=True) # Creation date
updated_at = models.DateTimeField(auto_now=True) # Updation Date
user = models.ForeignKey(get_user_model(),
on_delete=models.DO_NOTHING,related_name=โcategory_update_userโ, default=None, null=True) #User who updated the Article
def __str__(self):
return self.name ----------> this was an integer field initially in foreign reference
- [Django]-Passing arguments to a dynamic form in django
- [Django]-Django form with choices but also with freetext option?
- [Django]-How can I subtract or add 100 years to a datetime field in the database in Django?
0๐
In my case, I had a different model that was called by another one that had the default snippet from VSCode as:
def __str__(self):
return
This was causing the error, even when it was showing in the log another place.
- [Django]-Add data to ModelForm object before saving
- [Django]-Django โ Site matching query does not exist
- [Django]-Comparing querysets in django TestCase
0๐
Toto_tico is right, although the above error can also occur when you fail to return a str property of your django model within the str(self) method:
Class model and(models.Model):
Class Meta:
verbose_name = "something"
title = models.CharField(max_length=100)
email = models.CharField(max_length=100)
#Error instance:
def __str__(self):
self.title
#Right way
def __str__(self):
return self.title
So it is highly advised to return the str property within the
def str(self)
method to avoid errors in the Django admin panel when trying to make changes to a particular data within a model table:
def __str__(self):
return self.property
I hope this will help someone.
- [Django]-How to simplify migrations in Django 1.7?
- [Django]-Python coverage badges, how to get them?
- [Django]-AttributeError: 'module' object has no attribute 'setdefaultencoding'