@user_passes_test(lambda u: u.is_staff, login_url='/pyramid/login/') def defaulters_report(request): template = 'private/admin/report_defaulters.html' queryset = list(TreeNode.objects.all()) for x in queryset: x.d = x.debt defaulters = TreeNode.objects.filter(id__in=([x.id for x in queryset if x.d > 0])) context = dict() unpaid_purepro = defaulters[0].annuities.all() for x in list(defaulters)[1:]: unpaid_purepro = unpaid_purepro | x.annuities.all() unpaid_purepro = unpaid_purepro.filter(expected_date__lt=timezone.now()) context['total'] = unpaid_purepro.all().aggregate(Sum('total'))['total__sum'] return render(request, template, context)
When I try to get value of unpaid_purepro - I get the error as in title of question. Final error of view is:
Expression tree is too large (maximum depth 1000)
Where I am wrong?
In other words: for each TreeNode FilterPayment with last expected_date should be selected and TreeNodes should be ordered by their's payment__expected_date
UPD:
models.py:
class TreeNode(MPTTModel): class Meta: verbose_name = 'ΡΡΠ°ΡΡΠ½ΠΈΠΊ' verbose_name_plural = 'ΡΡΠ°ΡΡΠ½ΠΈΠΊΠΈ' # account for auth account = models.OneToOneField(User, verbose_name='Π°ΠΊΠΊΠ°ΡΠ½Ρ', related_name='treenode') @property def debt(self): ... @property def last_payment(self): return self.annuities.last() @property def pay_progress(self): return "{}/{}".format(self.annuities.exclude(fact_date=None).aggregate(Sum('total'))['total__sum'], self.annuities.aggregate(Sum('total'))['total__sum']) class FilterPayment(models.Model): class Meta: verbose_name = 'Π²Π·Π½ΠΎΡ Π·Π° ΡΠΈΠ»ΡΡΡ' verbose_name_plural = 'Π²Π·Π½ΠΎΡΡ Π·Π° ΡΠΈΠ»ΡΡΡ' expected_date = models.DateField(verbose_name='ΠΎΠΆΠΈΠ΄Π°Π΅ΠΌΠ°Ρ Π΄Π°ΡΠ°') fact_date = models.DateField(verbose_name='ΡΠ°ΠΊΡΠΈΡΠ΅ΡΠΊΠ°Ρ Π΄Π°ΡΠ°', null=True, blank=True) total = models.IntegerField(verbose_name='ΡΡΠΌΠΌΠ°') client = models.ForeignKey(TreeNode, related_name='annuities', verbose_name='ΠΊΠ»ΠΈΠ΅Π½Ρ') CASH = 1 TERMINAL = 2 METHOD_CHOICES = ( (CASH, "Cash"), (TERMINAL, "Mobilnik") ) method = models.PositiveSmallIntegerField(choices=METHOD_CHOICES, default=1)
score:1
Accepted answer
You get this error, because some of generated SQL queries are too large for SQLite. If you need sum of annuities
with past expected_date
of treenodes
with positive debt
, you can try this:
@user_passes_test(lambda u: u.is_staff, login_url='/pyramid/login/')
def defaulters_report(request):
template = 'private/admin/report_defaulters.html'
context = {}
context['total'] = TreeNode.objects.filter(
debt__gt=0,
annuities__expected_date__lt=timezone.now()
).aggregate(total=Sum('annuities__total'))['total']
return render(request, template, context)
Documentation:
Credit To: stackoverflow.com
Related Query
- Unable to get repr for <class 'django.db.models.QuerySet'> while aggregating data
- get class name for empty queryset in django
- Django: Assigning ForeignKey - Unable to get repr for class
- Shows "Unable to get repr for <class 'django.db.models.query.QuerySet'>" while retrieving data
- How to write manager class to set default attribute value for models while retrieval/filtering in Django queries?
- How to get two models data in for loop with filter: Django
- Django - For loop brings QuerySet but can't get individual data (as if blank) in HTML template
- Get a django queryset that includes data from 5 linked models
- Django get all descendant child models using django queryset
- Django Models for Time Series Data
- django test - how to get response data for future use
- Django how to get multiple context_object_name for multiple queryset from single view to single template
- Performance Optimisation for connecting data in django models
- Django Queryset - get last related object and filter by it's data
- Providing initial data for models - Django
- Iterating over a large Django queryset while the data is changing elsewhere
- In Django 1.6.x how to get data of associated models in views?
- Get all children models from a parent class - Django
- is it possible to use javascript to get data from django models db?
- How to get list instead of QuerySet class list in Django 2.2.6 version?
- Get data from multiple django app models on single html page
- Python Django Best Practice for request.user in Class Based Views and Queryset
- Django admin.site.register throws TypeError for models class
- Django Models Manager for saving custom processed data in a field
- Django - How to get only 2 object for a combination of fields with a queryset
- How to get a current date for upload_to in Django models
- How do get custom pagination class working for django rest framework v3.6
- Create a django queryset for non-database data
- How to show error message while save data from admin side.? i using models.model for create models
- all: unable to get repr for <class 'django.db.models.query.QuerySet'>
More Query from same tag
- TypeError: 'GeoQuerySet' object is not callable
- Executing code for a custom Django 404 page
- South and django-allauth: how can I get all tables to be synced?
- What is a signal in Django
- Applications in subfolder in 1.7
- Django Python ... Updating multiple elements in database
- django how to pull multiple querysets for a webpage
- NoneType object is not callable when adding saved FK
- Mezzanine web content language translations
- Customizable Authencation backends not followed by Django's own login testcases. Why?
- Template file path
- Heroku deployment error: The requested API endpoint was not found
- Can the Admin UI be used without the user authenticating in Django 1.3?
- Saving BytesIO to Django ImageField
- How to store complex csv data in django?