AttributeError when trying to reference ForeignKey attributes

27 views
Skip to first unread message

Robbie Edwards

unread,
May 24, 2014, 11:28:17 PM5/24/14
to django...@googlegroups.com
Hi, I have some code like such...


def gen_planet_pos( max_dim ):
    return random.randint( 0, max_dim )


class Universe( models.Model ):
    width = models.IntegerField( default=100 )
    ...


class Planet( models.Model ):
    universe = models.ForeignKey( Universe )
    ...
    x_pos = models.IntegerField( default=gen_planet_pos( universe.width ) )


When I try to use this, I get "AttributeError: 'ForeignKey' object has no attribute 'width'".  That message appears when I try to even enter the shell.  I'm not sure what to do to prevent this error.  Your help is appreciated.

robbie 

Kelvin Wong

unread,
May 25, 2014, 1:31:20 AM5/25/14
to django...@googlegroups.com
Might try removing the default from x_pos then try setting it by overriding the save method


K
Message has been deleted

Robbie Edwards

unread,
May 25, 2014, 3:52:20 PM5/25/14
to django...@googlegroups.com
That should work.  Thanks for the suggestion.

robbie
Reply all
Reply to author
Forward
0 new messages