简体   繁体   中英

How get Instance in Django ModelForm

I'm trying to update a model in Django using the class-based generic view UpdateView.But i have some instance problem.When i click submit button id is pass to update form , but instances isn't

i am new in django ,so please be forgiving if I'm doing something stupid.

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

Here is some part of my update_form.html .The rest part is the same

SOLUTION

Problem occured from my update button form's method.its method was "post".I change it to "get" method and problem solved.Don't need to overried Updateview post function.Thanks to all for solutions.

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 %}........... 

You shouldn't usually need to override post for generic class based views. The class based view will take care of fetching the instance if you simplify it to:

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

You don't need to add instance to the template context. The update view will automatically adds it, so you can use {{ object }} or {{ hardwares }} in the template ( hardwares is a bit confusing because it's a single object -- it would be better to follow the Django recommendation and name your model Hardware )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM