22👍
You can annotate the fields as you want, with the F expression:
from django.db.models import F
models.Table.objects.all().values('m', 'b').annotate(n=F('m'), a=F('b'))
8👍
Although this could have been done before by using extra(select={'n':'m','a':'b'})
, I agree that this really should have been a part of values()
itself.
To that end, and inspired by Alex’s ticket, I have just posted a patch that adds this feature. I hope you’ll find it useful!
- Django favicon.ico in development?
- Django: Uploading multiple files. List of files needed in cleaned_data['file']
5👍
Your reason in the comment makes no sense. Each field in the model has its own column in the database, and there’s never any danger of mixing them up. You can of course tell a field to use a column name that’s different from the field name:
myfield = models.CharField(max_length=10, db_column='differentname')
but I don’t know if that will help you, because I still don’t know what your problem is.
- Issue with Django 2.0 : 'WSGIRequest' object has no attribute 'session'
- Nullable TextField or empty string in Django model?
1👍
I’m presuming this isn’t possible, so I’ve raised a ticket for the feature to be added, I think there is some merit to being able to do this. Please see the ticket for more information.
- How to send success message if we use django generic views
- Getting Type error while opening an uploaded CSV File
- How to setup health check page in django
1👍
you can also use .alias() in your queryset.
.alias(alias=SomeExpression()).annotate(
SomeOtherExpression('alias'))
.alias(alias=SomeExpression()).order_by('alias')
.alias(alias=SomeExpression()).update(field=F('alias'))
for your specific case, this would be the answer
models.Table.objects.all().alias(
n=F('m'), a=F('b')).values('m', 'a')
)
- How to make GET CORS request with authorization header
- How to do pagination using mongoengine?
- Docker Alpine: Error loading MySQLdb module
0👍
I really can’t understand what you are trying to do however it sounds like what you are looking for is the extra queryset method. This for most purposes acts in the same manner as AS does in sql.