Nie brakuje w moim implementacji z tym błędem

Model:

class Person(models.Model):
    last_name = models.CharField(max_length=256)
    first_name = models.CharField(max_length=256)

    class Meta:
        abstract = True

class Supplier(Person):

    def __init__(self):
        super(Supplier, self).__init__()

class Item(models.Model):
    name = models.CharField(max_length=256)
    supplier = models.ForeignKey(Supplier)
    ...

def __unicode__(self):
    return self.name

Wyświetlenia:

def ItemNew(request):
    if request.method == "POST":
        post_item = ItemNewForm(request.POST)
        ...
    else:
        item_form = ItemNewForm()

        return render(request, "item_new.html", {
                'item_form' : item_form,
        })

Formularze:

class ItemNewForm(forms.ModelForm):
    class Meta:
        model = Item

HTML:

...
<form method="POST" id="item_new_form">
{% csrf_token %}
<label>Name : </label><<span>{{ item_form.name }}</span></span>
<label>Supplier : </label><span>{{ item_form.supplier }}</span>
<input type="submit" value="Add">
</form>
...

Ślad:

TypeError at /item/add
__init__() takes exactly 1 argument (7 given)
Request Method: GET
Request URL:    
Django Version: 1.5.4
Exception Type: TypeError
Exception Value:    
__init__() takes exactly 1 argument (7 given)
Exception Location: C:\Python27\lib\site-packages\django\db\models\query.py in iterator, line 327
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.5
Python Path:    
['E:\\projects\\WebPOS',
 'E:\\tools\\ide\\eclipse\\plugins\\org.python.pydev_3.0.0.201311051910\\pysrc',
 'E:\\projects\\WebPOS',
 'C:\\Python27\\lib\\site-packages\\distribute-0.6.49-py2.7.egg',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info',
 'C:\\WINDOWS\\SYSTEM32\\python27.zip',
 'C:\\Python27\\lib\\plat-win']
Server time:    Tue, 10 Dec 2013 07:45:07 +0800


Error during template rendering:

In template E:\projects\WebPOS\base\templates\item_new.html, error at line 14
__init__() takes exactly 1 argument (7 given)

4   
5   {% block sidebar %}
6   {% include "nav.html" %}
7   {% endblock %}
8   
9   {% block content %}
10  <h3>Add New Item</h3>
11  <form method="POST" id="item_new_form">
12      {% csrf_token %}
13      <div class="span-4"><label>Name : </label></div><div class="span-12 last"><span>{{ item_form.name }}</span></div>
14      <div class="span-4"><label>Supplier : </label></div><div class="span-12 last"><span>{{ item_form.supplier }}</span></div>
15      <div class="span-16 last"><input type="submit" value="Add"></div>
16  </form>
17  {% endblock %}
18  
3
27.neil 10 grudzień 2013, 04:03

2 odpowiedzi

Najlepsza odpowiedź

Problem polega na

class Supplier(Person):

    def __init__(self):
        super(Supplier, self).__init__()

Spójrz na Django.db.models. Kod źródłowy modelu

class Model(six.with_metaclass(ModelBase)):
    _deferred = False

    def __init__(self, *args, **kwargs):

Funkcja __init__ Uzyskaj tylko samą instancję jako argument, ale Django przechodzi prawdopodobnie więcej argumentów. Dlatego musisz użyć *args, **kwargs.

Aby lepiej zrozumieć *args i **kwargs można spojrzeć na To

7
Community 23 maj 2017, 12:33

Naprawiając ten błąd, zaktualizuj model dostawcy, dodając parametr {x0}}

class Supplier(Person):

    def __init__(self, *args, **kwargs):
        super(Supplier, self).__init__(*args, **kwargs)
4
27.neil 10 grudzień 2013, 05:49