Comments (3)
I think you can accomplish want you want already because you can pivot on an annotated column.
from django.db.models import Case, When, CharField
annotation = {
'pivot_column': Case(When(Q(stage='negotiating', is_archived=False), then=Value('Negotiating')),
When(Q(...), then=Value('Closed'),
When(Q(...), then=Value('Archived'),
default=None,
output_field=CharField()
}
queryset = Lead.objects.annotate(**annotation)
pivot_table = pivot(queryset, 'sales_rep', 'pivot_column', 'id', aggregation=Count)
If there is a cleaner API than the above, maybe it's worth having the pivot package expose that. I'm certainly open to making pivoting on an annotated column cleaner, but I'm not thinking of something better off the top of my head. The cases
list in your example isn't that different than what's in the annotation.
from django-pivot.
@wasabigeek I forgot to say, let me know if the above works for you or not.
from django-pivot.
@martsberger it works! Just a few typos in the example e.g. When
was not closed properly.
I was able to create the "Whens" programmatically and use those :)
whens = [
When(Q(stage='negotiating', is_archived=False), then=Value('Negotiating')),
When(Q(...), then=Value('Closed')),
When(Q(...), then=Value('Archived')),
]
...
queryset = Lead.objects.annotate(pivot_column=Case(*whens, default=None, output_field=CharField())
from django-pivot.
Related Issues (20)
- Example to render pivot table in template HOT 11
- Allow default values if no records are found HOT 2
- unexpected type conversion HOT 3
- Blocker - No readme.srt found in package HOT 1
- force_text in django_pivot.utils.py is depricated for django >=3.0 HOT 1
- Django Rest FrameWorks HOT 2
- Missing files in sdist
- is it possible to remove doublescore for lookup columns? HOT 2
- Total column HOT 1
- ModuleNotFoundError: No module named 'django_pivot' HOT 3
- TypeError: annotate() keywords must be strings + FIX HOT 2
- New version
- Bug with binary fields HOT 1
- row_range not working HOT 3
- Pivot on Union Queryset : Calling QuerySet.distinct() after union() is not supported. HOT 1
- Column values that contain spaces don't work (and doc might improve a smidgeon) HOT 1
- Queryset and pivot table ordering HOT 1
- Cumulative aggregation
- Add Support for multiples data fields HOT 2
- Pivot returns List instead of ValuesQueryset HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-pivot.