Abstract base class with foreign Key

1,448 views
Skip to first unread message

saeb

unread,
Aug 13, 2008, 12:35:59 PM8/13/08
to Django users
So, I have 2 abstract classes and 2 other classes inherit from these
abstract base classes.

--- /userApp.models.py ----
class Account (models.Model):
name = models.textfield()
balance = models.DecimalField(max_digits=16, decimal_places=7)
class Meta :
abstract = True

class User(models.Model):
name = models.Textfield()
user_account = models.ForeignKey(Account)
class Meta:
abstract = True

---- /paperUserApp.models.py -----

from UserApp.models import User

class Account(Account):
class Meta:
verbose_name ='paper account'
class Paper_User(User):
class Meta:
verbose_name ='paper user'

and I get these errors.

File "/Users/saeb/Desktop/Django-dir/paperUserApp/models.py", line 12,
in ?
from UserApp.models import User
File "/Users/saeb/Desktop/Django-dir/UserApp/models.py", line 119,
in ?
class Account(models.Model):
File "/Users/saeb/Desktop/Django-dir/UserApp/models.py", line 121,
in Account
user_account = models.ForeignKey(Account)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/
lib/python2.4/site-packages/django/utils/maxlength.py", line 47, in
inner
func(self, *args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/
lib/python2.4/site-packages/django/db/models/fields/related.py", line
626, in __init__
to_field = to_field or to._meta.pk.name
AttributeError: 'NoneType' object has no attribute 'name'.

I can't make sense of why it sees none object instead of account. What
does this error actually mean. What am I missing? Thanks in advance..

Chris Pickett

unread,
Aug 13, 2008, 12:34:05 PM8/13/08
to django...@googlegroups.com
Assuming your code isn't a typo, you're not importing the Account model in paperUserApp, just the User.

from UserApp.models import User

should be

rom UserApp.models import User, Account

Malcolm Tredinnick

unread,
Aug 13, 2008, 12:42:49 PM8/13/08
to django...@googlegroups.com

On Wed, 2008-08-13 at 09:35 -0700, saeb wrote:
> So, I have 2 abstract classes and 2 other classes inherit from these
> abstract base classes.
>
> --- /userApp.models.py ----
> class Account (models.Model):
> name = models.textfield()
> balance = models.DecimalField(max_digits=16, decimal_places=7)
> class Meta :
> abstract = True
>
> class User(models.Model):
> name = models.Textfield()
> user_account = models.ForeignKey(Account)
> class Meta:
> abstract = True
[...]

An "Account" isn't really a model. You cannot refer to it with a
ForeignKey. The reason is because it will never exist in isolation. You
can't create an Account instance. If you tried to write something like
ForeignKey(str) you would see a similar error. It's hard to handle every
single unexpected thing people do. Sometimes the mere fact that a huge
exception occurs is the clue that you need to look at the code you've
written.

Regards,
Malcolm


saeb

unread,
Aug 13, 2008, 12:52:35 PM8/13/08
to Django users

Nevermind, I found the the answer. Abstract class can't have foreign
key relationship to another abstract class.

if anyone is interested, read this discussion

http://groups.google.com.mx/group/django-users/browse_thread/thread/2c7e6412170124d5/67b8e00bea6a896b

thanks for reading
Reply all
Reply to author
Forward
0 new messages