用django createview从表单和其他字段保存对象

发布于 2025-02-03 18:35:54 字数 2168 浏览 1 评论 0 原文

我对如何在my_model中添加的其他字段,这些字段 在我的html表单中被排除在 forms.py 中,同时用 > CreateView

models.py:views.py:mymodel.html

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})

都是

class MyModelCreateView(CreateView):
    model= My_model
    form_class = MyModelForm
    template_name = "application/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:urls.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 
        }

urlpatterns=[
    path('demand', views.MyModelCreateView.as_view(), name='patern_name'),
path('detils', views.MyModelDetailView.as_view(), name='redirectin_pattern_name'),
]

I'm confused about how to add additional fields in my_model that are excluded from my HTML form defined in forms.py while saving with 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>

here everything is working fine the form is showing and I can submit data and save it to the database then I get automatically redirected to the details page of the object I saved and all the details are correct I even checked passing ids of another object to see if it changes and show the details of each object called and it works too.

now I just want to save the remaining attributes of the model at the same time when I'm saving the form to the database.

as requested
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'),
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2025-02-10 18:35:55

解决的

感谢我解决的互动,但我无法解释代码,因此我只会发布它。

我对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)

Solved

thanks for the interactions I solved it but I can't explain the code so I will just post it.

I made changes to 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文