django - python: model: can an attribute reference an attribute from the same model? -
(this question win dumbiest question prize, i'll give try)
i have simple menu class in models:
class menu(models.model): name = models.charfield(null=false, blank=false,unique=true,max_length=50) url = models.charfield(null=false, blank=false, unique=true,max_length=100) sortorder = models.integerfield(null=false, blank=false, default=0)
this happily let me build 1 level of menu items.
if wanted go further , add level, how make items reference , id same model , make sure id not id of same item?
so there 2 questions:
1. how set foreignkey , id same class? referencing neither menu or self won't validated:
class menu(models.model): name = models.charfield(null=false, blank=false,unique=true,max_length=50) url = models.charfield(null=false, blank=false, unique=true,max_length=100) sortorder = models.integerfield(null=false, blank=false, default=0) **parent = models.foreignkey(menu)**
will throw:
nameerror: name 'menu' not defined
2. how make sure item's id not id of item?
try self
parent = models.foreignkey( 'self' )
Comments
Post a Comment