Passing Instance before create, in a ForeignKeyField, Django -


i have 2 models in these forms:

class parent(models.model):    ....  class children(models.model):    parent = foreignkey('parent')    .... 

when creating children, have pass parent parent, problem want create children itself, when creating parent, other fields, simple "models.textfield" can lead me how that?

overwrite __init__() method of children models , overwrite save() method of parent model:

__init__:

# in class children() def __init__(self, parentid):     self.parent = parentid     # ... 

save:

# in class parent() def save(self, ...):     # parent save     # parent super().save()      # pseudo code #      if haschildren:         each childen of parent:             child = children(self.id) # <-- entails overwrite __init__() method in class children             child.save() 

additionally need render option (children-fields) create children parent.

if using django-admin interface only, can add inline models parent smodeladmin`. see django docs


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -