0

This are my models:

class company(models.Model):
    User = models.ForeignKey(User,related_name="Company_Owner",on_delete=models.CASCADE,null=True,blank=True)
    Name = models.CharField(max_length=50,blank=False)

class group1(models.Model):
    User = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
    group_Name = models.CharField(max_length=32)
    Company = models.ForeignKey(company,on_delete=models.CASCADE,null=True,blank=True,related_name='Company_group') 

class ledger1(models.Model):
    User            = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
    Company         = models.ForeignKey(company,on_delete=models.CASCADE,null=True,blank=True,related_name='Companys')

I have done this in my view:

from accounting_double_entry.models import group1,ledger1

model = company
paginate_by = 10

def get_queryset(self):
    return company.objects.filter(User=self.request.user).order_by('id')

def get_context_data(self, **kwargs):
    context = super(companyListView, self).get_context_data(**kwargs)
    context['selectdates'] = selectdatefield.objects.filter(User=self.request.user)
    groupcach = group1.objects.filter(User=self.request.user, Company=company.pk,Master__group_Name__icontains='Capital A/c')
    groupcacb = groupcach.annotate(
        closing = Coalesce(Sum('ledgergroups__Closing_balance'), 0))

    groupcstcb = groupcacb.aggregate(the_sum=Coalesce(Sum('closing'), Value(0)))['the_sum']

    ledcah = ledger1.objects.filter(User=self.request.user, group1_Name__group_Name__icontains='Capital A/c')   
    ledcacb = ledcah.aggregate(the_sum=Coalesce(Sum('Closing_balance'), Value(0)))['the_sum']

    total_cacb = groupcstcb + ledcacb
    context['capital'] = total_cacb
    return context

But getting this error:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'property'

I just cannot understand what this error mean. I just want to display the capital amount for all companies...

The error is in this line of code:

        groupcach = group1.objects.filter(User=self.request.user, Company=company.pk,Master__group_Name__icontains='Capital A/c')

Do anyone have any idea what I am doing wrong in my code?

5
  • where is group1 defined? Commented Dec 18, 2018 at 11:57
  • What is group1? Is it model name? Please provide info about it. Commented Dec 18, 2018 at 11:59
  • Yeah it is a model name...I have edited the question.. Commented Dec 18, 2018 at 12:00
  • Please show group1 model. As a note it's bad practice to name classes with lowecase. Use uppercase for that as Group. Commented Dec 18, 2018 at 12:01
  • Sorry for that...I have edited my question Commented Dec 18, 2018 at 12:02

1 Answer 1

3

The variable company seems to be a class (from the line model = company). You should always use capital letters for your class names (i.e. name your model Company not company).

This line:

groupcach = group1.objects.filter(User=self.request.user, Company=company.pk,Master__group_Name__icontains='Capital A/c')

uses company.pk so pk is the property field on your class company. You haven't anywhere defined an object company.

My advice:

  • Go back to refactor all your code to follow proper python conventions: classes are CamelCased, variables, properties and functions are snake_cased.
  • Don't mix the two: group_Name or Closing_balance are wrong.
  • related_name for a ForeignKey should be a plural.
  • Then rerun your code and the errors will be more obvious (company not defined).

The reason for these convention is that they make it easier to avoid mistakes later on.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm afraid that recommending PEP-8 is a hopeless battle: stackoverflow.com/a/52603345/67579, three months ago, this was already an issue (and pointed out by several answers by different authors). Since nothing is done about it, of course the trouble remains.

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.