Opened 3 years ago

Closed 3 years ago

#30898 closed Bug (invalid)

Multi FormSet form Error object has no attribute instance.

Reported by: azharsaleem9 Owned by: azharsaleem9
Component: Forms Version: 2.2
Severity: Normal Keywords: Formsets, forms, object has no attribute instance
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm working with multi Formset and get this problem. I mentioned my code blow please check and help me to solve this issue. Thanks

'VoucherEnteryForm' object has no attribute 'instance'

views.py

def index(request):
    voucherEnteryFormset = modelformset_factory(
        VoucherEntery, form=VoucherEnteryForm, fields=('group', 'subGroup', 'obtainMarks', 'totalMarks', 'grade'))
    form = JournalVoucherForm(request.POST or None)
    formset = voucherEnteryFormset(
        request.POST or None, queryset=VoucherEntery.objects.none(), prefix='jounarlvoucher')
    if request.method == 'POST':
        if form.is_valid() and formset.is_valid():
            try:
                with transaction.atomic():
                    voucher = form.save(commit=False)
                    voucher.save()

                    for entery in formset:
                        data = entery.save(commit=False)
                        data.voucher = voucher
                        data.save()
            except IntegrityError:
                print('Error Encountered')
            return redirect('home-page')
    context = {
        'formset': formset,
        'form': form
    }
    return render(request, 'myform.html', context)

models.py

class JounarlVoucher(models.Model):
    name = models.CharField(max_length=50)
    datevc = models.DateField(auto_now=False)
    reference = models.CharField(max_length=10)
    jrcode = models.IntegerField()
    createdAt = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "JounarlVoucher"


class VoucherEntery(models.Model):
    voucher = models.ForeignKey(
        JounarlVoucher, related_name='jounarlvoucher', on_delete=models.CASCADE)
    group = models.CharField(max_length=50)
    subGroup = models.CharField(max_length=50)
    obtainMarks = models.IntegerField()
    totalMarks = models.IntegerField()
    grade = models.CharField(max_length=10)
    createdAt = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.voucher

    class Meta:
        db_table = "VoucherEntery"

forms.py

class JournalVoucherForm(forms.Form):
    name = forms.CharField(label='Name', required=True, widget=forms.TextInput(
        attrs={
            'class': 'form-control',
            'placeholder': 'Name'
        }
    ))
    datevc = forms.DateField(label='Date', input_formats=['%Y/%m/%d'], widget=forms.DateTimeInput(
        attrs={
            'class': 'form-control',
        }
    ))
    reference = forms.CharField(label='Ref', required=False, widget=forms.TextInput(
        attrs={
            'class': 'form-control',
        }
    ))
    jrcode = forms.IntegerField(label='Code', required=False, widget=forms.NumberInput(
        attrs={
            'class': 'form-control',
        }
    ))


class VoucherEnteryForm(forms.Form):
    group = forms.ChoiceField(label='Group', required=True, choices=myGroup, widget=forms.Select(
        attrs={
            'class': 'form-control formset-field',
        }
    ))
    subGroup = forms.ChoiceField(label='Sub Group', required=True, choices=subGroup, widget=forms.Select(
        attrs={
            'class': 'form-control formset-field',
        }
    ))
    obtainMarks = forms.IntegerField(label='Obtain Marks', required=True, widget=forms.NumberInput(
        attrs={
            'class': 'form-control formset-field',
        }
    ))
    totalMarks = forms.IntegerField(label='Total Marks', required=True, widget=forms.NumberInput(
        attrs={
            'class': 'form-control formset-field',
        }
    ))
    grade = forms.CharField(label='Grade', required=True, widget=forms.TextInput(
        attrs={
            'class': 'form-control formset-field',
        }
    ))

HTML Template

<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}

    {% for data_frm in formset %}
    <tr class="item">
        <td>{{ data_frm.group }}</td>
        <td>{{ data_frm.subGroup }}</td>
        <td>{{ data_frm.obtainMarks }}</td>
        <td>{{ data_frm.totalMarks }}</td>
        <td>{{ data_frm.grade }}</td>
        <td>
            <button class="btn btn-danger remove-form-row">Delete</button>
        </td>
    </tr>
    {% endfor %}

    <button type="button" class="btn btn-success add-form-row" id="{{ formset.prefix }}">
        Add
    </button>
    {{ formset.management_form }}
    <button type="submit" class="btn btn-success">Submit</button>
</form>

Change History (2)

comment:1 Changed 3 years ago by azharsaleem9

Owner: changed from nobody to azharsaleem9
Status: newassigned

comment:2 Changed 3 years ago by Mariusz Felisiak

Resolution: invalid
Status: assignedclosed
Summary: Multi FormSet form Error object has no attribute instanceMulti FormSet form Error object has no attribute instance.

Please don't use trac as a support channel.

Closing per TicketClosingReasons/UseSupportChannels.

Note: See TracTickets for help on using tickets.
Back to Top