39๐
It took about 90 minutes to find this.
I only found it after I had taken out of my model first the abstract supermodel, then all relationship fields, then all-but-one data fields, until only a single IntegerField
was left. The create
still didnโt work.
At that point I called create
on some other simple model class MyModel2
in exactly the same test context. It worked (like the idiomatic breeze).
So what the hell was special about MyModel
??
And then it dawned on me: MyModel
had an __init__
method; most of my other models did not. So look at that. And bang your forehead: I simply had forgotten the mandatory (in Python 3 style)
super().__init__(*args, **kwargs)
Moral: Donโt forget this or you may suffer from a really tough error message.
(Note: If you donโt like the style of this post, I am sorry. It was required therapeutic writing for me.)
5๐
As for me, I had:
def __init__(self):
return self.title
instead of
def __str__(self):
return self.title
so I had mistyped __init__
in place of __str__
- Why swagger raises unclear error โ Django
- Use a django built in filter in code (outside of a template)
- Can Django run on Gunicorn alone (no Apache or nginx)?
1๐
to reformulate the previous and make it more explicit:
this error is due to an __init__
method that is not referencing its parent class.
make sure your __init__
method is defined like this:
def __init__(self, *args, **kwargs):
super().__init__(*args,**kwargs)
...
- Django AttributeError 'tuple' object has no attribute 'regex'
- Remove padding from matplotlib plotting
0๐
I ran into this error, but from a completely different use case. I ended up breaking my Django model instance object by removing the id
attribute from its __dict__
representation, then later trying to access the id
attribute again as shown below. Solution: do not remove items from your Django model instanceโs __dict__
if you plan to use those attributes later on the original instance object.
instance1 = File.objects.get(path = '/path/to/sample1.txt')
print(instance1)
# /path/to/sample1.txt # this is my normal __str__ method for the model
# preview the instance dict representation
print(instance1.__dict__)
# {'_state': <django.db.models.base.ModelState object>, 'id': 7, 'path': '/path/to/sample1.txt'}
# create object to hold the dict
instance1_dict = instance1.__dict__
# remove 'id' for test case because its non-deterministic
instance1_dict.pop('id')
# check what the dict object looks like now
print(instance1_dict)
# {'_state': <django.db.models.base.ModelState object>, 'path': '/path/to/sample1.txt'}
# 'id' no longer present
# check what the original instance object dict representation looks like
print(instance1.__dict__)
# {'_state': <django.db.models.base.ModelState object>, 'path': '/path/to/sample1.txt'}
# 'id' is no longer present anymore
# try to access 'id' on the original instance object
expected_id = instance1.id
# AttributeError: 'NoneType' object has no attribute 'attname'
0๐
This error can occur with django proxy model inheritance. For example, let assume that app have normal django model Report
and proxy model ReportExist
inherited from it. Error occurs when another normal model (ReportSelect
) is inherited from proxy model:
class Report(models.Model):
# normal model
class ReportExist(Report):
class Meta:
proxy = True
class ReportSelect(ReportExist):
# no meta, error
Adding a Meta
class with proxy = True
to the ReportSelect
resolves the error:
class ReportSelect(ReportExist):
class Meta:
proxy = True
# no error
- Django redirect() with anchor (#) parameters
- What does it mean for an object to be unscriptable?
- Subclassing Django ModelForms
- Appropriate choice of authentication class for python REST API used by web app
0๐
This is more a comment than a solution but I donโt have enough reputation to comment so need to put it like this. In our case, the problem was that we used Python f-strings in logger calls, which contained Django model objects as vars. The problem was 100% reproducible. After replacing f-stings with recommended string formatting style for Python lazy logger (e.g. logger.info('Hello %s', name)
instead of logger.info(f'Hello {name}')
) the issue was gone. I hope Iโll save someone from hours of t-shooting.
- Paypal monthly subscription plan settings for first day of the month and making monthly recurring payment โ django python
- Django render_to_string() ignores {% csrf_token %}