UpdateView不保存到数据库

UpdateView不保存到数据库

本文介绍了Django-UpdateView不保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击提交"按钮后,我的UpdateView不会保存到本地数据库.

My UpdateView does not save to local DB upon clicking Submit button.

views.py

class BHA_UpdateView(UpdateView):
    template_name = 'bha_test.html'
    context_object_name = 'bha'
    model = BHA_overall
    success_url = reverse_lazy('well_list')
    pk_url_kwarg = 'pk_alt'
    form_class = BHA_overall_Form

    def get_object(self, queryset=None):
        pk = self.kwargs.get(self.pk_url_kwarg)
        api = get_well_api(self.request)
        current_bha = BHA_List.objects.filter(well=api, id=pk)[0]
        return current_bha

forms.py

class BHA_overall_Form(forms.ModelForm):
    class Meta():
        model = BHA_overall
        fields = '__all__'

models.py

class BHA_List(models.Model):
    well = models.ForeignKey(WellInfo, 'CASCADE', related_name='bha_list')
    bha_number = models.CharField(max_length=100)

class BHA_overall(models.Model):
    bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_overall')
    drill_str_name = models.CharField(max_length=111)
    depth_in = models.CharField(max_length=111)

bha_test.html

<form method="POST">
  {% csrf_token %}
  {{ form.as_p }}
  <input type="submit" class='btn btn-primary' value="Submit">
</form>

有人告诉我,在UpdateView中,模型和表单应引用相同的内容.所以我做到了,但它仍然无法正常工作.这是我的原始问题:

Someone told me that in UpdateView, the model and the form should refer to the same thing. So I did that, but its still not working. Here is my original question:

如何修改代码,以便将表单保存到数据库中?

How can I modify my code so that my form will save to my DB?

更新:

BHA_List具有BHA对象的列表.每个BHA对象都有许多子对象,BHA_overall是其中的一个.

BHA_List has a list of BHA objects. Each BHA objects has many child objects, BHA_overall being one of it.

我的网址看起来像这样:

I have url that looks like this:

re_path(r'^bha/(?P<pk_alt>[-\w]+)$', base_views.BHA_UpdateView.as_view(), name='bha')

理想情况下,pk_url_kwarg = 'pk_alt'应该在BHA_List中查找对象,然后导航到一个页面,您可以在其中更新有关BHA_List中该特定对象的信息.您可以更新的字段包括BHA_overall中的字段.

Ideally, pk_url_kwarg = 'pk_alt' should look up objects inside BHA_List, and navigate to a page where you can update information about that specific object inside BHA_List. The fields you can update include the fields in BHA_overall.

所以看起来像这样:

You can now edit [ BHA 2 ]: # url = .../bha/2

    BHA Overall Information -
    Drill_str_name: []
    Depth_in: []
    ...

    Other Child Model -
    other_field: []
    other_field: []

我需要pk_url_kwarg = 'pk_alt'来查询BHA_List实例以生成唯一的url,并且我需要BHA_overall_Form显示用户可以编辑的输入字段,并将该用户输入保存到DB.那我还要在这里使用什么CBV?

I need pk_url_kwarg = 'pk_alt' to query BHA_List instances to generate unique url, and I need BHA_overall_Form to display input fields where users can edit, and save that user input to DB. What other CBV should I use here then?

推荐答案

您的get_object方法返回BHA_list对象.但是您的视图和表单的模型是BHA_overall.因此,get_object还应该返回要更新的BHA_overall实例:

Your get_object method returns BHA_list object. But your view and form's model is BHA_overall. So get_object should also return BHA_overall instance which you want to update:

def get_object(self, queryset=None):
    pk = self.kwargs.get(self.pk_url_kwarg)
    api = get_well_api(self.request)
    bha_number = BHA_List.objects.filter(well=api, id=pk)[0]
    current_bha = BHA_overall.objects.filter(bha_number=bha_number)[0]
    return current_bha

这篇关于Django-UpdateView不保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 16:35