python django sort with lambda with if statement
I have date and some dollar gross model:
class FirstDate(models.Model):
gross = models.DecimalField(max_digits=12, decimal_places=2, default=0)
updated = models.DateTimeField(auto_now=True)
class SecondDate(models.Model):
gross = models.DecimalField(max_digits=12, decimal_places=2, default=0)
updated = models.DateTimeField(auto_now=True)
And want to sort it by gross
, and if gross
is the same, then sort it with updated
field
For example,
qs1 = SoloDate.objects.all()[:2]
qs2 = GroupDate.objects.all()[:2]
result_list = sorted(
chain(qs1, qs2),
key=lambda x: x.gross # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
I mean, let me say that there is two objects individually from qs1 and qs2.
# objects from qs1
qs1_obj1 = {
'pk': 1,
'gross': 5,
'updated': 2018-11-24 10:53:23.360707+00:00
}
qs1_obj2 = {
'pk': 2,
'gross': 5,
'updated': 2018-11-25 10:53:23.360707+00:00
}
# objects from qs2
qs2_obj1 = {
'pk': 3,
'gross': 5,
'updated': 2018-11-24 10:53:23.360707+00:00
}
qs2_obj2 = {
'pk': 4,
'gross': 1,
'updated': 2018-11-23 10:53:23.360707+00:00
}
It's result_list
order will be qs1_obj1
, qs2_obj1
, qs1_obj2
, qs_2_obj_2
.
Reasons is it:
qs1_obj1
: 1.by gross, 2.by updated, 3.by pk,
qs2_obj1
: 1.by gross, 2.by updated, 3. but pk was not good,
qs1_obj2
: 1.by gross, 2.but by dpdated was late,
qs2_obj2
: 1.gross was small.
Maybe it is not good question or bothersome question, I need help.
Question line is that :
key=lambda x: x.gross # and if gross is the same, for the same gross objects, x.updated and then update was also the same, x.pk,
How can I do this?
python django lambda
add a comment |
I have date and some dollar gross model:
class FirstDate(models.Model):
gross = models.DecimalField(max_digits=12, decimal_places=2, default=0)
updated = models.DateTimeField(auto_now=True)
class SecondDate(models.Model):
gross = models.DecimalField(max_digits=12, decimal_places=2, default=0)
updated = models.DateTimeField(auto_now=True)
And want to sort it by gross
, and if gross
is the same, then sort it with updated
field
For example,
qs1 = SoloDate.objects.all()[:2]
qs2 = GroupDate.objects.all()[:2]
result_list = sorted(
chain(qs1, qs2),
key=lambda x: x.gross # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
I mean, let me say that there is two objects individually from qs1 and qs2.
# objects from qs1
qs1_obj1 = {
'pk': 1,
'gross': 5,
'updated': 2018-11-24 10:53:23.360707+00:00
}
qs1_obj2 = {
'pk': 2,
'gross': 5,
'updated': 2018-11-25 10:53:23.360707+00:00
}
# objects from qs2
qs2_obj1 = {
'pk': 3,
'gross': 5,
'updated': 2018-11-24 10:53:23.360707+00:00
}
qs2_obj2 = {
'pk': 4,
'gross': 1,
'updated': 2018-11-23 10:53:23.360707+00:00
}
It's result_list
order will be qs1_obj1
, qs2_obj1
, qs1_obj2
, qs_2_obj_2
.
Reasons is it:
qs1_obj1
: 1.by gross, 2.by updated, 3.by pk,
qs2_obj1
: 1.by gross, 2.by updated, 3. but pk was not good,
qs1_obj2
: 1.by gross, 2.but by dpdated was late,
qs2_obj2
: 1.gross was small.
Maybe it is not good question or bothersome question, I need help.
Question line is that :
key=lambda x: x.gross # and if gross is the same, for the same gross objects, x.updated and then update was also the same, x.pk,
How can I do this?
python django lambda
add a comment |
I have date and some dollar gross model:
class FirstDate(models.Model):
gross = models.DecimalField(max_digits=12, decimal_places=2, default=0)
updated = models.DateTimeField(auto_now=True)
class SecondDate(models.Model):
gross = models.DecimalField(max_digits=12, decimal_places=2, default=0)
updated = models.DateTimeField(auto_now=True)
And want to sort it by gross
, and if gross
is the same, then sort it with updated
field
For example,
qs1 = SoloDate.objects.all()[:2]
qs2 = GroupDate.objects.all()[:2]
result_list = sorted(
chain(qs1, qs2),
key=lambda x: x.gross # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
I mean, let me say that there is two objects individually from qs1 and qs2.
# objects from qs1
qs1_obj1 = {
'pk': 1,
'gross': 5,
'updated': 2018-11-24 10:53:23.360707+00:00
}
qs1_obj2 = {
'pk': 2,
'gross': 5,
'updated': 2018-11-25 10:53:23.360707+00:00
}
# objects from qs2
qs2_obj1 = {
'pk': 3,
'gross': 5,
'updated': 2018-11-24 10:53:23.360707+00:00
}
qs2_obj2 = {
'pk': 4,
'gross': 1,
'updated': 2018-11-23 10:53:23.360707+00:00
}
It's result_list
order will be qs1_obj1
, qs2_obj1
, qs1_obj2
, qs_2_obj_2
.
Reasons is it:
qs1_obj1
: 1.by gross, 2.by updated, 3.by pk,
qs2_obj1
: 1.by gross, 2.by updated, 3. but pk was not good,
qs1_obj2
: 1.by gross, 2.but by dpdated was late,
qs2_obj2
: 1.gross was small.
Maybe it is not good question or bothersome question, I need help.
Question line is that :
key=lambda x: x.gross # and if gross is the same, for the same gross objects, x.updated and then update was also the same, x.pk,
How can I do this?
python django lambda
I have date and some dollar gross model:
class FirstDate(models.Model):
gross = models.DecimalField(max_digits=12, decimal_places=2, default=0)
updated = models.DateTimeField(auto_now=True)
class SecondDate(models.Model):
gross = models.DecimalField(max_digits=12, decimal_places=2, default=0)
updated = models.DateTimeField(auto_now=True)
And want to sort it by gross
, and if gross
is the same, then sort it with updated
field
For example,
qs1 = SoloDate.objects.all()[:2]
qs2 = GroupDate.objects.all()[:2]
result_list = sorted(
chain(qs1, qs2),
key=lambda x: x.gross # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
I mean, let me say that there is two objects individually from qs1 and qs2.
# objects from qs1
qs1_obj1 = {
'pk': 1,
'gross': 5,
'updated': 2018-11-24 10:53:23.360707+00:00
}
qs1_obj2 = {
'pk': 2,
'gross': 5,
'updated': 2018-11-25 10:53:23.360707+00:00
}
# objects from qs2
qs2_obj1 = {
'pk': 3,
'gross': 5,
'updated': 2018-11-24 10:53:23.360707+00:00
}
qs2_obj2 = {
'pk': 4,
'gross': 1,
'updated': 2018-11-23 10:53:23.360707+00:00
}
It's result_list
order will be qs1_obj1
, qs2_obj1
, qs1_obj2
, qs_2_obj_2
.
Reasons is it:
qs1_obj1
: 1.by gross, 2.by updated, 3.by pk,
qs2_obj1
: 1.by gross, 2.by updated, 3. but pk was not good,
qs1_obj2
: 1.by gross, 2.but by dpdated was late,
qs2_obj2
: 1.gross was small.
Maybe it is not good question or bothersome question, I need help.
Question line is that :
key=lambda x: x.gross # and if gross is the same, for the same gross objects, x.updated and then update was also the same, x.pk,
How can I do this?
python django lambda
python django lambda
edited Nov 24 '18 at 11:43
touchingtwist
asked Nov 24 '18 at 11:37
touchingtwisttouchingtwist
572520
572520
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Try sorting by multiple fields like so:
result_list = sorted(
chain(qs1, qs2),
key=lambda x: (x.gross, -x.updated.timestamp(), x.pk) # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
Thank you but I think, it seems that I need therereverse updated
. Because last updated one come as first. How can I do this?-x.updated
returns line:TypeError: bad operand type for unary -: 'datetime.datetime'
– touchingtwist
Nov 24 '18 at 11:49
1
@touchingtwist I've updated the answer. datetime is now converted to timestamp which is an integer.
– Kamil Niski
Nov 24 '18 at 11:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457718%2fpython-django-sort-with-lambda-with-if-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try sorting by multiple fields like so:
result_list = sorted(
chain(qs1, qs2),
key=lambda x: (x.gross, -x.updated.timestamp(), x.pk) # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
Thank you but I think, it seems that I need therereverse updated
. Because last updated one come as first. How can I do this?-x.updated
returns line:TypeError: bad operand type for unary -: 'datetime.datetime'
– touchingtwist
Nov 24 '18 at 11:49
1
@touchingtwist I've updated the answer. datetime is now converted to timestamp which is an integer.
– Kamil Niski
Nov 24 '18 at 11:53
add a comment |
Try sorting by multiple fields like so:
result_list = sorted(
chain(qs1, qs2),
key=lambda x: (x.gross, -x.updated.timestamp(), x.pk) # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
Thank you but I think, it seems that I need therereverse updated
. Because last updated one come as first. How can I do this?-x.updated
returns line:TypeError: bad operand type for unary -: 'datetime.datetime'
– touchingtwist
Nov 24 '18 at 11:49
1
@touchingtwist I've updated the answer. datetime is now converted to timestamp which is an integer.
– Kamil Niski
Nov 24 '18 at 11:53
add a comment |
Try sorting by multiple fields like so:
result_list = sorted(
chain(qs1, qs2),
key=lambda x: (x.gross, -x.updated.timestamp(), x.pk) # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
Try sorting by multiple fields like so:
result_list = sorted(
chain(qs1, qs2),
key=lambda x: (x.gross, -x.updated.timestamp(), x.pk) # and if gross is the same, for the gross same objects, x.updated and then update was also the same, x.pk,
reverse=True
)
edited Nov 24 '18 at 11:52
answered Nov 24 '18 at 11:43
Kamil NiskiKamil Niski
2,6341314
2,6341314
Thank you but I think, it seems that I need therereverse updated
. Because last updated one come as first. How can I do this?-x.updated
returns line:TypeError: bad operand type for unary -: 'datetime.datetime'
– touchingtwist
Nov 24 '18 at 11:49
1
@touchingtwist I've updated the answer. datetime is now converted to timestamp which is an integer.
– Kamil Niski
Nov 24 '18 at 11:53
add a comment |
Thank you but I think, it seems that I need therereverse updated
. Because last updated one come as first. How can I do this?-x.updated
returns line:TypeError: bad operand type for unary -: 'datetime.datetime'
– touchingtwist
Nov 24 '18 at 11:49
1
@touchingtwist I've updated the answer. datetime is now converted to timestamp which is an integer.
– Kamil Niski
Nov 24 '18 at 11:53
Thank you but I think, it seems that I need there
reverse updated
. Because last updated one come as first. How can I do this? -x.updated
returns line: TypeError: bad operand type for unary -: 'datetime.datetime'
– touchingtwist
Nov 24 '18 at 11:49
Thank you but I think, it seems that I need there
reverse updated
. Because last updated one come as first. How can I do this? -x.updated
returns line: TypeError: bad operand type for unary -: 'datetime.datetime'
– touchingtwist
Nov 24 '18 at 11:49
1
1
@touchingtwist I've updated the answer. datetime is now converted to timestamp which is an integer.
– Kamil Niski
Nov 24 '18 at 11:53
@touchingtwist I've updated the answer. datetime is now converted to timestamp which is an integer.
– Kamil Niski
Nov 24 '18 at 11:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457718%2fpython-django-sort-with-lambda-with-if-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown