1

got an error when post the data

AttributeError at /system/create/
type object 'object' has no attribute 'EmpInstallment'
Request Method: POST
Request URL:    http://127.0.0.1:8000/system/create/
Django Version: 1.11.2
Exception Type: AttributeError
Exception Value:    
type object 'object' has no attribute 'EmpInstallment'
Exception Location: /Users/wakanda/pyproject/payroll/system/views.py in form_valid, line 117
Python Executable:  /Users/wakanda/py-virtualenv/payroll3/bin/python
Python Version: 3.6.5
Python Path:    
['/Users/wakanda/pyproject/payroll',
 '/Users/wakanda/py-virtualenv/payroll3/lib/python36.zip',
 '/Users/wakanda/py-virtualenv/payroll3/lib/python3.6',
 '/Users/wakanda/py-virtualenv/payroll3/lib/python3.6/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Users/wakanda/py-virtualenv/payroll3/lib/python3.6/site-packages',
 '/Users/wakanda/py-virtualenv/payroll3/lib/python3.6/site-packages/setuptools-39.0.1-py3.6.egg',
 '/Users/wakanda/py-virtualenv/payroll3/lib/python3.6/site-packages/configparser-3.5.0-py3.6.egg']

model.py

class Employee(models.Model):
    nik = models.CharField(max_length=100)
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("system:detail",kwargs={'pk':self.pk})

class EmpLoan(models.Model):
    status = models.BooleanField()
    nominal = models.DecimalField(max_digits=10, decimal_places=0)
    emp = models.ForeignKey(Employee, related_name='emploan')
    created_at = models.DateTimeField(auto_now=True)
    updated_at = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return self.emp.name

class EmpInstallment(models.Model):
    nominal = models.DecimalField(max_digits=10, decimal_places=0)
    loan = models.ForeignKey(EmpLoan, related_name='empinstallment')
    created_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.loan.emp.name

view.py

class EmployeeCreateView(CreateView):
    fields = ('spl', 'status')
    model = models.Employee

    def form_valid(self, form):
        self.object = form.save(commit=False)

        try:
            pinjaman = models.EmpLoan.objects.get(emp_id=1, status=0)
            cicilans = models.EmpInstallment.objects.filter(loan_id=pinjaman.id).values_list('id', flat=True)
            totalcicilan = 0
            for i in cicilans:
                cicilan = models.EmpInstallment.objects.get(id=i).nominal
                totalcicilan = totalcicilan + cicilan

            sisapinjaman = pinjaman.nominal - totalcicilan

            if ( 12 - cicilans.count()) != 0:
                cicilansekarang = sisapinjaman / ( 12 - cicilans.count())
            else:
                cicilansekarang = 0

            #potongan cicilan
            hasil = round(cicilansekarang)

            if hasil != 0:
                joe = models.EmpInstallment.objects.create(loan=pinjaman, nominal=hasil)
                object.EmpInstallment.add(joe)
            else:
                statatus_pinjaman = models.EmpLoan.objects.get(emp_id=1, status=0)
                statatus_pinjaman.status = 1
                statatus_pinjaman.save()

        except models.EmpLoan.DoesNotExist:
            hasil = 0

        return super(ModelFormMixin, self).form_valid(form)

when I click submit on my form, its showing error but checked on my database it's submitted.

what wrong with my code?...

especially on

joe = models.EmpInstallment.objects.create(loan=pinjaman, nominal=hasil)
object.EmpInstallment.add(joe)

please explain because I am a slow learner and super noob on Django & OOP.

3 Answers 3

5

object is Python's buitin. I suppose you need to use self object:

self.object.EmpInstallment.add(joe)
Sign up to request clarification or add additional context in comments.

7 Comments

can I use self.object also when using 2 models or more?... I mean when I click submit on my form, 1 data going to model 1, and another submitted to second or more model.
now 'Employee' object has no attribute 'EmpInstallment' on same line. since my class EmployeeCreateView(CreateView): add data to 2 models. Employee and EmpInstallment and they not related. Employee -> EmpLoan -> EmpInstallment
@DickyRaambo self.object is instance of Employee class. So it doen't have EmpInstallment atribute. Probably you want to add manytoone relation between Employee and EmpInstallment? In this case you should add employee = models.ForeignKey(Employee, related_name='EmpInstallment'). After makemigrations` and migrate your code should work
if my scheme like this Employee -> EmpLoan -> EmpInstallment how do it?... so EmpInstallment have foreign to EmpLoan and EmpLoan foreign to Employee
@DickyRaambo eploan=self.object.emploan.first() and then eploan.empinstallment.add(joe). Now first emploan related to employee will contain new object. But it's not very good solution. Instead of it i recommend you to rewrite your scheme as i suggested.
|
1

Firstly, If you do this:

self.object = form.save(commit=False)

You need to call the save method when you're done, like:

self.object.EmpInstallment.add(joe)
self.object.save()

But, I would advice you change the name from self.object to something else because it could confuse you and might conflict with python. Even though it is an object, you could give it a better name like new_emp_installment.

Lastly, this line:

return super(ModelFormMixin, self).form_valid(form)

Should be:

return super(CreateView, self).form_valid(form)

Comments

0

Issue is in your this line.

object.EmpInstallment.add(joe)

when you use object then it is considered as python's built-in object.

You should use like

self.object.field_name.add(joe)

that will be

self.object.emp.add(joe)

then it will add mentioned model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.