Django - multiple models in one page

不羁的心 提交于 2020-01-16 20:57:48

问题


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 is a parent model, and the rest are child models related by ForeignKey. The screenshot is the page I want to create

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

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

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.

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!


回答1:


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

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.


来源:https://stackoverflow.com/questions/51528054/django-multiple-models-in-one-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!