使用 django createview 从表单和其他字段保存对象
save an object with django createview from a form and additional fields
我对如何在 my_model 中添加其他字段感到困惑,这些字段已从我的 HTML 中排除 ] 在 forms.py 中定义的表单,同时使用 createview!
保存
models.py:
class My_model(models.Model):
# attrs in the form that are saved already with now problem
...
#attrs excluded in forms.py but I want to save them along with the form
attr_1=models.CharField(max_length=30)
attr_2_type=[('a','A'),('b','B')]
attr_2=models.Charfield(max_length=8,choices=attr_2_type)
...
def get_absolute_url(self):
return reverse("application:redirectin_pattern_name", kwargs={"pk":self.pk})
views.py:
class MyModelCreateView(CreateView):
model= My_model
form_class = MyModelForm
template_name = "application/mymodel.html"
mymodel.html
<div class="row ">
<div class="col-md-3"></div>
<div class="col-md-6">
{% block form %}
<form method="post" action="{% url 'application:pattern_name' %}">
{% endblock form %}
{% csrf_token %}
{{ form.as_p }}
<input class="form-control btn-primary" type="submit" name="save"
value="send your demand" />
</form>
</div>
</div>
此处显示的一切正常,我可以提交数据并将其保存到数据库,然后我会自动重定向到我保存的对象的详细信息页面,所有详细信息都是正确的我什至检查了传递的 ID另一个对象以查看它是否更改并显示调用的每个对象的详细信息并且它也有效。
现在我只想在将表单保存到数据库的同时保存模型的其余属性。
根据要求
forms.py:
from .models import My_Model
class MyModelForm(ModelForm):
class Meta:
model=My_Model
exclude=("attr_1", "attr_2")
widgets={
# i listed all the attributes that I need the user to
# fill and they all are being saved correctly
}
urls.py
urlpatterns=[
path('demand', views.MyModelCreateView.as_view(), name='patern_name'),
path('detils', views.MyModelDetailView.as_view(), name='redirectin_pattern_name'),
]
已解决
感谢您的互动,我解决了它,但我无法解释代码,所以我将 post 它。
我更改了 views.py
class MyModelCreateView(CreateView):
model= My_model
form_class = MyModelForm
template_name = "application/mymodel.html"
def form_valid(self, form):
# comment: this fn will return the data from the HTML form
# and add to it the missing attrs of the model then save it
form.instance.attr_1='string I want to save'
#to save a clean string pass it between '' not ""
form.instance.attr_2='a'
return super().form_valid(form)
我对如何在 my_model 中添加其他字段感到困惑,这些字段已从我的 HTML 中排除 ] 在 forms.py 中定义的表单,同时使用 createview!
保存models.py:
class My_model(models.Model):
# attrs in the form that are saved already with now problem
...
#attrs excluded in forms.py but I want to save them along with the form
attr_1=models.CharField(max_length=30)
attr_2_type=[('a','A'),('b','B')]
attr_2=models.Charfield(max_length=8,choices=attr_2_type)
...
def get_absolute_url(self):
return reverse("application:redirectin_pattern_name", kwargs={"pk":self.pk})
views.py:
class MyModelCreateView(CreateView):
model= My_model
form_class = MyModelForm
template_name = "application/mymodel.html"
mymodel.html
<div class="row ">
<div class="col-md-3"></div>
<div class="col-md-6">
{% block form %}
<form method="post" action="{% url 'application:pattern_name' %}">
{% endblock form %}
{% csrf_token %}
{{ form.as_p }}
<input class="form-control btn-primary" type="submit" name="save"
value="send your demand" />
</form>
</div>
</div>
此处显示的一切正常,我可以提交数据并将其保存到数据库,然后我会自动重定向到我保存的对象的详细信息页面,所有详细信息都是正确的我什至检查了传递的 ID另一个对象以查看它是否更改并显示调用的每个对象的详细信息并且它也有效。
现在我只想在将表单保存到数据库的同时保存模型的其余属性。
根据要求 forms.py:
from .models import My_Model
class MyModelForm(ModelForm):
class Meta:
model=My_Model
exclude=("attr_1", "attr_2")
widgets={
# i listed all the attributes that I need the user to
# fill and they all are being saved correctly
}
urls.py
urlpatterns=[
path('demand', views.MyModelCreateView.as_view(), name='patern_name'),
path('detils', views.MyModelDetailView.as_view(), name='redirectin_pattern_name'),
]
已解决
感谢您的互动,我解决了它,但我无法解释代码,所以我将 post 它。
我更改了 views.py
class MyModelCreateView(CreateView):
model= My_model
form_class = MyModelForm
template_name = "application/mymodel.html"
def form_valid(self, form):
# comment: this fn will return the data from the HTML form
# and add to it the missing attrs of the model then save it
form.instance.attr_1='string I want to save'
#to save a clean string pass it between '' not ""
form.instance.attr_2='a'
return super().form_valid(form)