gpt4 book ai didi

Adding fields to a form and a model dynamically in django(在Django中向表单和模型动态添加字段)

转载 作者:bug小助手 更新时间:2023-10-25 13:04:59 24 4
gpt4 key购买 nike



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,但我猜这肯定是解决它的最简单的方法)


更多回答
优秀答案推荐
更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com