When clicking on “save” (after creating a new instance) in the Django Admin Site I get an
AttributeError at /admin/bankaccount/bank_account/add/
'Bank_Account' object has no attribute 'id'
I did not create any IDs in the model. The tutorial says that IDs are auto-managed.
Any idea why I get this?
My Model:
class Bank_Account(models.Model):
iban = models.CharField(max_length = 30)
bic = models.CharField(max_length = 30)
def __init__(self):
self.iban = 0
self.bic = 0
My admin.py
from bankaccount.models import Bank_Account
from django.contrib import admin
admin.site.register(Bank_Account)
I assume from your code sample that you’ve overridden the
__init__(constructor) in yourBank_Accountmodel. However you haven’t called the super. Actually, the actions you take should be expressed as defaults in the model:which should work fine. However I’m not sure why you want those as defaults; I’d have thought that no default and
null=False, blank=Falseis more appropriate in this case as a bank account will surely always have both IBAN and BIC. Perhaps I’m misunderstanding your domain problem there, though.The key thing is: don’t override constructors without ensuring you do all the work of the constructor in your superclass(es).