Django-一页中包含多个模型 [英] Django - multiple models in one page

查看:88
本文介绍了Django-一页中包含多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的模型:

I have a model that looks like this:

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_Drill_Bit(models.Model):
    bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_drill_bit')
    bit_type = models.CharField(max_length=111)  

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

class BHA_Motor(models.Model):
    bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_drill_bit')
    motor_type = models.CharField(max_length=111)

BHA_List 是父模型,其余是与 ForeignKey 相关的子模型。屏幕快照是我要创建的页面

BHA_List is a parent model, and the rest are child models related by ForeignKey. The screenshot is the page I want to create

因此,我想使用 model = BHA_List 中的一个实例生成基本页面。在此页面中,我要通过 ForeignKey 关系编辑与 BHA_List 相关的模型实例。

So, I want to generate a base page using one of the instances in model = BHA_List. In this page, I want to edit model instances that are related to BHA_List by ForeignKey relationship.

我目前有一个看起来像这样的视图,但是它是错误的:

I currently have a view that looks like this, but its wrong:

class BHA_UpdateView(UpdateView):
    model = BHA_List
    pk_url_kwarg = 'pk_alt'
    form_class = BHA_overall_Form

通过设置 model = BHA_List ,我能够在 BHA_List 中获得一个实例,并从中生成网址。现在,我的视图正确返回 BHA_List 中的一个实例: BHA 1

By setting model = BHA_List, I was able to get one of the instances in BHA_List, and generate url from it. Right now my views correctly return one of the instances in BHA_List: BHA 1

我尝试通过设置 form_class = BHA_overall_Form 来编辑子模型。但这没有任何作用,尽管它在用户端显示了表单字段。编辑并单击提交按钮后,所做的更改不会保存在数据库中。有人指出这是因为在我设置 model = BHA_List 时,我在 UpdateView 和表单中的模型不匹配,但 form_class = BHA_overall_form

I attempted to edit child models by setting form_class = BHA_overall_Form. But this doesn't do anything, though it displayed form fields on the user side. After editing and clicking Submit button, the changes are not saved in DB. Someone pointed out that this is because my model in UpdateView and form does not match, as I set model = BHA_List, but form_class = BHA_overall_form.

如何解决此问题?其他人也指出了使用多种视图的方法,但是我真的不知道该怎么做,因为我是Django的新手。任何帮助将不胜感激。谢谢!

How can I resolve this issue? Someone else also pointed out using multiple views, but I don't really know how to do it, as I'm very new to Django. Any help will be greatly appreciated. Thanks!

推荐答案

就这样,您就知道了。如果要更新一个表中的一行,则可以使用UpdateView。当您将模型设置为BHA_LIST时,您是在说Django。嘿,Django,我想更新此模型,以便为我呈现一个包含此表中字段的表单。您可以通过仅在模型上设置字段attr来完成此操作,也可以像使用表格那样自定义显示哪些字段。现在,允许设置自己的表单的好处是。尽管我们创建了modelForm,但也可以在其中添加其他字段。现在您的BHAOverallForm应该看起来像这样,以容纳您需要的所有字段。

Just so that you know. UpdateView can be used if you want to update a single row in one table. When you set model = BHA_LIST you are saying Django. Hey, Django I want to update this model so render me a form with the fields from this table. You can do this by just setting fields attr on the model or use a form like you did which will customize which fields are shown. Now the good thing about allowing to set our own form is. Though we create a modelForm we can also add extra fields inside it. Now your BHAOverallForm should look like this to accommodate all the fields you need.

forms.py

class BHAOverallForm(forms.ModelForm):
    well = models.ForeignKey(WellInfo, 'CASCADE', related_name='bha_list')
    bha_number = models.CharField(max_length=100)
    bit_type = models.CharField(max_length=111
    drill_str_name = models.CharField(max_length=111)
    motor_type = models.CharField(max_length=111)

    class Meta:
        model = BHAList

您可以像在表格中那样使用此表格现在,您还可以添加clean_field来添加验证。现在进入更新部分,您的视图应该像这样

you can use this form inside your form like you do now. You can also add clean_field to add validations. Now coming to the update part. your views should look like this

views.py

class BHAUpdateView(UpdateView):
    model = BHAList
    form_class = BHAOverallForm

    def form_valid(self, form):
        super(BHAUpdateView, self).form_valid(form) # save BHAList to the DB
        bha_list = form.instance
        bha_drill_bit = bha_list.bhadrillbit_set.first() # assuming you have only one drill_bit per list, if you need more modify your question accordingly.
        bha_drill_bit.bit_type = form.cleaned_data.get("bit_type)
        bha_drill_bit.save()

        # you can do the same for other models as well.

这篇关于Django-一页中包含多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆