如何在Django ModelForm中获取实例

时间:2017-08-17 12:27:15

标签: python django django-models django-forms

我正在尝试使用基于类的通用视图UpdateView更新Django中的模型。但我有一些实例问题。当我单击提交按钮时,id传递给更新表单,但实例不是

我是django的新手,所以如果我做些蠢事,请原谅。

urls.py

app_name = 'inventory'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),

url(r'^inventory/(?P<pk>[0-9]+)/delete/$', views.HardwareDelete.as_view(), name='hardware-delete'),
url(r'^inventory/update/(?P<pk>[0-9]+)/$', views.HardwareUpdate.as_view(), name='hardware-update'),
# url(r'^/inventory/add$', views.InventoryAdd.as_view(), name='add-inventory'),]

models.py

class Hardwares(models.Model):
    hardware_unit=models.CharField(max_length=100)
    hardware_model=models.CharField(max_length=100)
    hardware_subsystem=models.CharField(max_length=100)
    hardware_serial_number=models.CharField(max_length=1000)
    hardware_manufacturer = models.CharField(max_length=1000)
    hardware_operating_system = models.CharField(max_length=1000)
    hardware_quantity = models.IntegerField(default=1, validators=[MinValueValidator(1)])

    def get_absolute_url( self ):
        return reverse('inventory:index')

    def __str__(self):
        return self.hardware_serial_number+"  "+self.hardware_model+"  "+self.hardware_unit+"  "+str(self.hardware_quantity)

forms.py

class HomeForm(forms.ModelForm):
    hardware_unit = forms.CharField(
    widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Hardware Unit Name..', }))
    hardware_model = forms.CharField(
    widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Model Name...', }))
    hardware_subsystem = forms.CharField(
    widget=forms.TextInput(attrs={'class': 'form-control', }))
    hardware_serial_number = forms.CharField(
    widget=forms.TextInput(attrs={'class': 'form-control', }))
    hardware_manufacturer = forms.CharField(
    widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Manufacturer Company Name', }))
    hardware_operating_system = forms.CharField(
    widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Operating System Name', }))
    hardware_quantity = forms.IntegerField(validators=[MinValueValidator(1)],
    widget=forms.NumberInput(attrs={'class': 'form-control', 'placeholder': 'Quantity of Harware', }))

    class Meta:
        model = Hardwares
        fields = {'hardware_unit', 'hardware_model', 'hardware_subsystem', 'hardware_serial_number',
              'hardware_manufacturer', 'hardware_operating_system', 'hardware_quantity', }

views.py

class HardwareUpdate(UpdateView):
    model = Hardwares
    template_name = 'inventory/update_form.html'
    form_class = HardwareForm

update.form html

{% extends 'inventory/basic_menu.html' %}
{% block body %}

                <form class="form-horizontal" action="" method="post"
                      enctype="multipart/form-data">
                    {% csrf_token %}


                    <div class="form-group">
                        <label class="col-md-3 control-label">Hardware
                            Unit</label>
                        <div class="col-md-9">
                            {{ form.hardware_unit }}

                            <span class="help-block">Hardware Unit Name eg.:  Firewall , Notebook , Phone</span>
                        </div>
                    </div>
             //Have more 8 form groups like this

{% endblock body %}

以下是我的update_form.html的一部分。其余部分是相同的

我的更新按钮窗体的方法出现问题。方法是“post”。我将其更改为“get”方法并解决了问题。不需要覆盖Updateview post功能。感谢所有人提供解决方案。

  

BEFOR

        <form action="{% url 'inventory:hardware-update' hardwares.id %}"
        method="post" style="display: inline;">
                            {% csrf_token %}...........
     

AFTER

        <form action="{% url 'inventory:hardware-update' hardwares.id %}"
        method="get" style="display: inline;">
                            {% csrf_token %}...........

1 个答案:

答案 0 :(得分:2)

对于基于泛型类的视图,通常不需要覆盖post。如果您将实例简化为:

,则基于类的视图将负责获取实例
class HardwareUpdate(UpdateView):
    model = Hardwares
    form_class = HomeForm
    template_name = 'inventory/update_form.html'

您无需将instance添加到模板上下文中。更新视图会自动添加它,因此您可以在模板中使用{{ object }}{{ hardwares }}hardwares有点令人困惑,因为它只是一个对象 - 最好遵循Django建议并命名您的模型Hardware