TheFireFox
TheFireFox

Reputation: 71

"MultipleObjectsReturned" from db on template

Updated:

with "item15 = Anhang.objects.filter(KN=item.KN)" i get the Error: Exception Type: AttributeError Exception Value:
'QuerySet' object has no attribute '_meta'

views.py

@login_required()
def anhang_view(request, id=None):
    contextoo = {}
    item = Kunden.objects.get(id=id)
    kontaktform_form = KontaktForm(request.POST or None, instance=item)
    creatorform_form = CreateANform()
    contextoo['creatorform_form'] = creatorform_form
    if Kunden.objects.filter(KN=item.KN).exists():
        item14 = Kunden.objects.get(KN=item.KN)
        editkontakto_form = InfoKontaktoForm(request.POST or None, instance=item14)
        contextoo['editkontakto_form'] = editkontakto_form
    if Anhang.objects.filter(KN=item.KN).exists():
        item15 = Anhang.objects.filter(KN=item.KN)
        ANform_form = ANform(request.POST or None, instance=item15)
        contextoo['ANform_form'] = ANform_form
    if request.method == 'POST':
        creatorform_form = CreateANform(request.POST)
        if creatorform_form.is_valid():
            cre = creatorform_form.save(commit=True)
            cre.save()
        return redirect('/Verwaltung/KontaktAnlegen')
    else:
        return render(request, 'blog/anhang.html', contextoo)

ERROR:

'QuerySet' object has no attribute '_meta' Request Method: GET Request URL: http://127.0.0.1:8000/Verwaltung/Anhang/10 Django Version: 3.0.1 Exception Type: AttributeError Exception Value: 'QuerySet' object has no attribute '_meta'

anhang.html

    .
    .
    .
{% if ANform_form %}
    {% for obj in ANform_form.instance %}
          <table class="table" width="100%" border="0" cellspacing="0" cellpadding="0">
            <thead class="thead-light">
                <tr>
                    <td width="11%" border="0" cellspacing="0" cellpadding="0">
                        <b>
                            {% csrf_token %}
                            {{ obj.Thema }}
                        </b>
                    </td>
                    <td width="15%" border="0" cellspacing="0" cellpadding="0">Username</td>
                    <td width="19%" border="0" cellspacing="0" cellpadding="0">Password</td>
                    <td width="18%" border="0" cellspacing="0" cellpadding="0">E-Mail</td>
                    <td width="37%" border="0" cellspacing="0" cellpadding="0">Anhang</td>
                    <td>
                    </td>
                    <td></td>
                </tr>
            </thead>
            <tbody>
                <td></td>
                <td>
                    {% csrf_token %}
                    {{ obj.Username }}
                </td>
                <td>
                    {% csrf_token %}
                    {{ obj.Password }}
                </td>
                <td>
                    {% csrf_token %}
                    {{ obj.Email }}
                </td>
                <td>
                    {% csrf_token %}
                    {{ obj.Anhang }}
                </td>
                <td>
                    <a href=""><button type="button" class="btn btn-danger">Löschen</button></a> </td>
                </td>
            </tbody>
          </table>
    {% endfor %}
{% endif %}

models.py

class Anhang(models.Model):
    KN = models.IntegerField(unique=False)
    Thema = models.CharField(max_length=100, blank=True, unique=False)
    Username = models.CharField(max_length=100, blank=True, unique=False)
    Password = models.CharField(max_length=100, blank=True, unique=False)
    Email = models.CharField(max_length=100, blank=True, unique=False)
    Anhang = models.CharField(max_length=250, blank=True, unique=False)

forms.py

class ANform(forms.ModelForm):

    class Meta:
        model = Anhang
        fields = ['KN', 'Thema', 'Username', 'Password', 'Email', 'Anhang']
        required = ()
        labels = {
            'KN': 'KN',
            'Thema': 'Thema:',
            'Username': 'Username',
            'Password': 'Password',
            'Email': 'Email',
            'Anhang': 'Anhang',
        }


class CreateANform(forms.ModelForm):

    class Meta:
        model = Anhang
        fields = ['KN', 'Thema', 'Username', 'Password', 'Email', 'Anhang']
        required = ()
        labels = {
            'KN': 'KN',
            'Thema': 'Thema:',
            'Username': 'Username',
            'Password': 'Password',
            'Email': 'Email',
            'Anhang': 'Anhang',
        }

class KontaktForm(forms.ModelForm):

    def clean_kundennummer(self):
        KN = self.cleaned_data['KN']
        if Kunden.objects.filter(KN_iexact=KN).exists():
            raise forms.ValidationError('Diese Kundennummer ist bereits vergeben')
        return KN

    class Meta:
        model = Kunden
        fields = ['KN', 'Anrede', 'Name', 'Vorname', 'Infos']
        required = ()
        labels = {
            'KN': 'Kundennummer',
            'Anrede': 'Anrede',
            'Name': 'Name',
            'Vorname': 'Vorname',
            'Infos': 'Infos',
        }

class InfoKontaktoForm(forms.ModelForm):

    def clean_kundennummer(self):
        KN = self.cleaned_data['KN']
        if Kunden.objects.filter(KN_iexact=KN).exists():
            raise forms.ValidationError('Diese Kundennummer ist bereits vergeben')
        return KN

    class Meta:
        model = Kunden
        fields = ['KN', 'Anrede', 'Name', 'Vorname']
        required = ()
        labels = {
            'KN': 'Kundennummer:',
            'Anrede': 'Anrede:',
            'Name': 'Name:',
            'Vorname': 'Vorname',
        }

how do i fix that? already tried with "get_list_or_404" & Anhang.objects.all()

Upvotes: 2

Views: 248

Answers (2)

Manan M.
Manan M.

Reputation: 1394

Actually all() function of django ORM is taking only one argument as self.

So, you can not pass queryset into all(). You can check here that how is working all() at line 909.

So, you have to replace your item15 with below...

item15 = Anhang.objects.filter(KN=item.KN)

For iterate loop in template you have to write below code...

{% for obj in ANform_form.instance %}
    {{ obj.Thema }}
    {{ obj.Username }}
{% endfor %}

I wrote simple for loop, you can write your html as per your requirements in for loop.

Upvotes: 1

KJTHoward
KJTHoward

Reputation: 876

You can't pass arguments to .all as it's meant to return all objects. You should replace

item15 = Anhang.objects.all(Anhang.objects.filter(KN=item.KN))

With

item15 = Anhang.objects.filter(KN=item.KN)

Alternatively, if there is only one item you can do

item15 = Anhang.objects.get(KN=item.KN)

Upvotes: 0

Related Questions