Increase date's month by interval in the model field within filter
up vote
0
down vote
favorite
I am getting objects like this. date_from, date_to are input variables
result = Records.objects.filter(recorddate__range=(date_from, date_to))
There is another integer variable off_set. How can I use it in filter like this
so the month of recorddate field is increased by off_set numbers. Like
result = Records.objects.filter((recorddate+off_set)__range=(date_from, date_to))
python django django-views
add a comment |
up vote
0
down vote
favorite
I am getting objects like this. date_from, date_to are input variables
result = Records.objects.filter(recorddate__range=(date_from, date_to))
There is another integer variable off_set. How can I use it in filter like this
so the month of recorddate field is increased by off_set numbers. Like
result = Records.objects.filter((recorddate+off_set)__range=(date_from, date_to))
python django django-views
What about adding it to bothdate_fromanddate_to?
– toti08
2 days ago
Is theoff_setinteger always the number of months to add?
– Will Keeling
yesterday
1
AS @toti08 mentioned, if you want to check if adate + offsetis within arange, you can also check thatdateis in therange - offset.
– dirkgroten
yesterday
Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
– lat long
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am getting objects like this. date_from, date_to are input variables
result = Records.objects.filter(recorddate__range=(date_from, date_to))
There is another integer variable off_set. How can I use it in filter like this
so the month of recorddate field is increased by off_set numbers. Like
result = Records.objects.filter((recorddate+off_set)__range=(date_from, date_to))
python django django-views
I am getting objects like this. date_from, date_to are input variables
result = Records.objects.filter(recorddate__range=(date_from, date_to))
There is another integer variable off_set. How can I use it in filter like this
so the month of recorddate field is increased by off_set numbers. Like
result = Records.objects.filter((recorddate+off_set)__range=(date_from, date_to))
python django django-views
python django django-views
asked 2 days ago
lat long
723314
723314
What about adding it to bothdate_fromanddate_to?
– toti08
2 days ago
Is theoff_setinteger always the number of months to add?
– Will Keeling
yesterday
1
AS @toti08 mentioned, if you want to check if adate + offsetis within arange, you can also check thatdateis in therange - offset.
– dirkgroten
yesterday
Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
– lat long
yesterday
add a comment |
What about adding it to bothdate_fromanddate_to?
– toti08
2 days ago
Is theoff_setinteger always the number of months to add?
– Will Keeling
yesterday
1
AS @toti08 mentioned, if you want to check if adate + offsetis within arange, you can also check thatdateis in therange - offset.
– dirkgroten
yesterday
Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
– lat long
yesterday
What about adding it to both
date_from and date_to?– toti08
2 days ago
What about adding it to both
date_from and date_to?– toti08
2 days ago
Is the
off_set integer always the number of months to add?– Will Keeling
yesterday
Is the
off_set integer always the number of months to add?– Will Keeling
yesterday
1
1
AS @toti08 mentioned, if you want to check if a
date + offset is within a range, you can also check that date is in the range - offset.– dirkgroten
yesterday
AS @toti08 mentioned, if you want to check if a
date + offset is within a range, you can also check that date is in the range - offset.– dirkgroten
yesterday
Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
– lat long
yesterday
Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
– lat long
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.
Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).
For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:
from dateutil.relativedelta import relativedelta
months = relativedelta(months=off_set)
result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.
Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).
For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:
from dateutil.relativedelta import relativedelta
months = relativedelta(months=off_set)
result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))
add a comment |
up vote
0
down vote
As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.
Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).
For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:
from dateutil.relativedelta import relativedelta
months = relativedelta(months=off_set)
result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))
add a comment |
up vote
0
down vote
up vote
0
down vote
As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.
Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).
For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:
from dateutil.relativedelta import relativedelta
months = relativedelta(months=off_set)
result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))
As mentioned in the comments, you could subtract the offset from the range to filter the relevant records.
Creating the offset could be slightly tricky, due to the fact that months have differing numbers of days (Python's timedelta doesn't support months).
For this you might consider dateutil.relativedelta(), although this would require a separate pip install python-dateutil. You'd also need to ensure that date_from and date_to were converted to datetime objects if they weren't already. For example:
from dateutil.relativedelta import relativedelta
months = relativedelta(months=off_set)
result = Records.objects.filter(recorddate__range=(date_from - months, date_to - months))
answered 16 hours ago
Will Keeling
9,88122329
9,88122329
add a comment |
add a comment |
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%2f53372806%2fincrease-dates-month-by-interval-in-the-model-field-within-filter%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
What about adding it to both
date_fromanddate_to?– toti08
2 days ago
Is the
off_setinteger always the number of months to add?– Will Keeling
yesterday
1
AS @toti08 mentioned, if you want to check if a
date + offsetis within arange, you can also check thatdateis in therange - offset.– dirkgroten
yesterday
Yes the off_set is always int num of months. Actually is is coming from another Model and may vary record to record
– lat long
yesterday