Lets say we have a model called SimpleModel and it has an attr connected_field which is ForeignKey of NotSimpleModel
i.e.
假设我们有一个名为SimpleModel的模型,它有一个attr Connected_field,它是NotSimpleModel的ForeignKey,即
class SimpleModel(models.Model):
connected_field = models.ForeignKey('NotSimpleModel', on_delete=models.CASCADE, related_name='connected')
class NotSimpleModel(models.Model):
...
and we have a form called NotSimpleForm
我们有一个叫做NotSimpleForm的表单
class NotSimpleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
...
class Meta:
model = NotSimpleModel
fields = '__all__'
Well, actually i wanna do a page form where user can dynamically add "connected_field" (not only this one field. Also fields from NotSimpleModel) to the form which is ModelForm of NotSimpleModel, then all the fields, which user added dynamically should be sent to the view and saved to the db (i doesnt insist on ForeignKey, but i guess it gotta be the easiest way to solve it tho)
嗯,实际上我想做一个页面形式,用户可以动态添加“连接的领域”(不只是这一个领域。还有从NotSimpleModel到NotSimpleModel的ModelForm的字段,然后用户动态添加的所有字段都应该发送到视图并保存到数据库中(我不坚持使用ForeignKey,但我猜这肯定是解决它的最简单的方法)
更多回答
优秀答案推荐
我是一名优秀的程序员,十分优秀!