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
s
modeladmin`. see django docs
Comments
Post a Comment