django 2: Filter models by every day of current monthHow do I do a not equal in Django queryset filtering?An issue filtering related models inside the model definitiondjango query filter conditionWhat is wrong with my models.py?Radio buttons in django adminCreate a new model which have all fields of currently existing modeldjango models request get id error Room matching query does not existDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerDjango auto_now=True for Timestamp not working when using normal mysql update queryDirect assignment to the forward side of a many-to-many set is prohibited. Use particular.set() instead
Why is my power MOSFET heating up when on?
Selecting by attribute using Python and a list
Why are Payments from Apple to New Zealand and Australian bank accounts wire transfers?
Professor Roman loves to teach unorthodox Chemistry
What is this Amiga 2000 mod?
Lowest Magnitude Eigenvalues of Large Sparse Matrices
What did the 8086 (and 8088) do upon encountering an illegal instruction?
How do I type a hyphen in iOS 12?
Why did Robert pick unworthy men for the White Cloaks?
How can powerful telekinesis avoid violating Newton's 3rd Law?
Course development: can I pay someone to make slides for the course?
Is it true that "only photographers care about noise"?
What is this object?
Why did the World Bank set the global poverty line at $1.90?
DateTime.addMonths skips a month (from feb to mar)
Am I allowed to determine tenets of my contract as a warlock?
Grandpa has another non math question
Why does there seem to be an extreme lack of public trashcans in Taiwan?
How does AFV select the winning videos?
If the pressure inside and outside a balloon balance, then why does air leave when it pops?
When to use и or а as “and”?
How to make a composition of functions prettier?
Oil draining out shortly after turbo hose detached/broke
Does a single fopen introduce TOCTOU vulnerability?
django 2: Filter models by every day of current month
How do I do a not equal in Django queryset filtering?An issue filtering related models inside the model definitiondjango query filter conditionWhat is wrong with my models.py?Radio buttons in django adminCreate a new model which have all fields of currently existing modeldjango models request get id error Room matching query does not existDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerDjango auto_now=True for Timestamp not working when using normal mysql update queryDirect assignment to the forward side of a many-to-many set is prohibited. Use particular.set() instead
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I got a simple models like this:
class CurrentMonthRegisterPage(models.Manager):
"""This manager class filter with current month."""
current_date = datetime.now().date()
def get_queryset(self):
return super(CurrentMonthRegisterPage, self).get_queryset().filter(
detail_hour__month=self.current_date.month, detail_hour__year=self.current_date.year)
class RegisterPage(models.Model):
OTHERS = '4'
EXTRA_TIME = '3'
EARLIER = '2'
ON_TIME = '1'
LATE = '0'
ABSENT = '-'
STATUS_LIST = (
(LATE, _("Late")),
(ON_TIME, _("On time")),
(EARLIER, _("Earlier")),
(EXTRA_TIME, _("Extra time")),
(OTHERS, _("ND")),
(ABSENT, _("Absent")),
)
detail_hour = models.DateTimeField(_('Date and hour'), auto_now_add=True)
details_mouvement = models.TextField(_("Déscription"), blank=True)
state = models.CharField(_("Statut"), max_length=1, choices=STATUS_LIST, default=ABSENT)
objects = RecentManager()
c_month = CurrentMonthRegisterPage()
Now i want to get a number of every state of every day of current month
Example:
Current month is March
How to get a number of state==LATE of every day of march ?
I want to get something like this:
queryset = ['late':[1,1,3,5,....31], 'other_state': [1,2,...], ...]
Please help ?
django django-models
add a comment |
I got a simple models like this:
class CurrentMonthRegisterPage(models.Manager):
"""This manager class filter with current month."""
current_date = datetime.now().date()
def get_queryset(self):
return super(CurrentMonthRegisterPage, self).get_queryset().filter(
detail_hour__month=self.current_date.month, detail_hour__year=self.current_date.year)
class RegisterPage(models.Model):
OTHERS = '4'
EXTRA_TIME = '3'
EARLIER = '2'
ON_TIME = '1'
LATE = '0'
ABSENT = '-'
STATUS_LIST = (
(LATE, _("Late")),
(ON_TIME, _("On time")),
(EARLIER, _("Earlier")),
(EXTRA_TIME, _("Extra time")),
(OTHERS, _("ND")),
(ABSENT, _("Absent")),
)
detail_hour = models.DateTimeField(_('Date and hour'), auto_now_add=True)
details_mouvement = models.TextField(_("Déscription"), blank=True)
state = models.CharField(_("Statut"), max_length=1, choices=STATUS_LIST, default=ABSENT)
objects = RecentManager()
c_month = CurrentMonthRegisterPage()
Now i want to get a number of every state of every day of current month
Example:
Current month is March
How to get a number of state==LATE of every day of march ?
I want to get something like this:
queryset = ['late':[1,1,3,5,....31], 'other_state': [1,2,...], ...]
Please help ?
django django-models
It is not very clear what you are trying to do.
– Stargazer
Mar 24 at 22:56
Look March month have 31 days, I want to get number of every state of every day of march.
– Nathan Ingram
Mar 24 at 23:03
add a comment |
I got a simple models like this:
class CurrentMonthRegisterPage(models.Manager):
"""This manager class filter with current month."""
current_date = datetime.now().date()
def get_queryset(self):
return super(CurrentMonthRegisterPage, self).get_queryset().filter(
detail_hour__month=self.current_date.month, detail_hour__year=self.current_date.year)
class RegisterPage(models.Model):
OTHERS = '4'
EXTRA_TIME = '3'
EARLIER = '2'
ON_TIME = '1'
LATE = '0'
ABSENT = '-'
STATUS_LIST = (
(LATE, _("Late")),
(ON_TIME, _("On time")),
(EARLIER, _("Earlier")),
(EXTRA_TIME, _("Extra time")),
(OTHERS, _("ND")),
(ABSENT, _("Absent")),
)
detail_hour = models.DateTimeField(_('Date and hour'), auto_now_add=True)
details_mouvement = models.TextField(_("Déscription"), blank=True)
state = models.CharField(_("Statut"), max_length=1, choices=STATUS_LIST, default=ABSENT)
objects = RecentManager()
c_month = CurrentMonthRegisterPage()
Now i want to get a number of every state of every day of current month
Example:
Current month is March
How to get a number of state==LATE of every day of march ?
I want to get something like this:
queryset = ['late':[1,1,3,5,....31], 'other_state': [1,2,...], ...]
Please help ?
django django-models
I got a simple models like this:
class CurrentMonthRegisterPage(models.Manager):
"""This manager class filter with current month."""
current_date = datetime.now().date()
def get_queryset(self):
return super(CurrentMonthRegisterPage, self).get_queryset().filter(
detail_hour__month=self.current_date.month, detail_hour__year=self.current_date.year)
class RegisterPage(models.Model):
OTHERS = '4'
EXTRA_TIME = '3'
EARLIER = '2'
ON_TIME = '1'
LATE = '0'
ABSENT = '-'
STATUS_LIST = (
(LATE, _("Late")),
(ON_TIME, _("On time")),
(EARLIER, _("Earlier")),
(EXTRA_TIME, _("Extra time")),
(OTHERS, _("ND")),
(ABSENT, _("Absent")),
)
detail_hour = models.DateTimeField(_('Date and hour'), auto_now_add=True)
details_mouvement = models.TextField(_("Déscription"), blank=True)
state = models.CharField(_("Statut"), max_length=1, choices=STATUS_LIST, default=ABSENT)
objects = RecentManager()
c_month = CurrentMonthRegisterPage()
Now i want to get a number of every state of every day of current month
Example:
Current month is March
How to get a number of state==LATE of every day of march ?
I want to get something like this:
queryset = ['late':[1,1,3,5,....31], 'other_state': [1,2,...], ...]
Please help ?
django django-models
django django-models
asked Mar 24 at 22:39
Nathan IngramNathan Ingram
207117
207117
It is not very clear what you are trying to do.
– Stargazer
Mar 24 at 22:56
Look March month have 31 days, I want to get number of every state of every day of march.
– Nathan Ingram
Mar 24 at 23:03
add a comment |
It is not very clear what you are trying to do.
– Stargazer
Mar 24 at 22:56
Look March month have 31 days, I want to get number of every state of every day of march.
– Nathan Ingram
Mar 24 at 23:03
It is not very clear what you are trying to do.
– Stargazer
Mar 24 at 22:56
It is not very clear what you are trying to do.
– Stargazer
Mar 24 at 22:56
Look March month have 31 days, I want to get number of every state of every day of march.
– Nathan Ingram
Mar 24 at 23:03
Look March month have 31 days, I want to get number of every state of every day of march.
– Nathan Ingram
Mar 24 at 23:03
add a comment |
1 Answer
1
active
oldest
votes
You need a query with fields for day and state, then you do a count (implicitly grouping by day and state):
from django.db.models import Count
from django.db.models.functions import Trunc
queryset = (RegisterPage.c_month
.annotate(day=Trunc('detail_hour', 'day'))
.values('day', 'state')
.annotate(count=Count('day'))
.order_by('day', 'state')
)
I've added an ordering clause to remove any existing ordering (that would thwart the desired grouping) and to sort the results.
The results only include days and states that are actually present in the data, if you want to include missing days or states with the count 0
, you may want to do it in Python code.
Yes want to include missing days or states with the count 0 how can i do it in python code ?
– Nathan Ingram
Mar 25 at 0:37
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%2f55329273%2fdjango-2-filter-models-by-every-day-of-current-month%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
You need a query with fields for day and state, then you do a count (implicitly grouping by day and state):
from django.db.models import Count
from django.db.models.functions import Trunc
queryset = (RegisterPage.c_month
.annotate(day=Trunc('detail_hour', 'day'))
.values('day', 'state')
.annotate(count=Count('day'))
.order_by('day', 'state')
)
I've added an ordering clause to remove any existing ordering (that would thwart the desired grouping) and to sort the results.
The results only include days and states that are actually present in the data, if you want to include missing days or states with the count 0
, you may want to do it in Python code.
Yes want to include missing days or states with the count 0 how can i do it in python code ?
– Nathan Ingram
Mar 25 at 0:37
add a comment |
You need a query with fields for day and state, then you do a count (implicitly grouping by day and state):
from django.db.models import Count
from django.db.models.functions import Trunc
queryset = (RegisterPage.c_month
.annotate(day=Trunc('detail_hour', 'day'))
.values('day', 'state')
.annotate(count=Count('day'))
.order_by('day', 'state')
)
I've added an ordering clause to remove any existing ordering (that would thwart the desired grouping) and to sort the results.
The results only include days and states that are actually present in the data, if you want to include missing days or states with the count 0
, you may want to do it in Python code.
Yes want to include missing days or states with the count 0 how can i do it in python code ?
– Nathan Ingram
Mar 25 at 0:37
add a comment |
You need a query with fields for day and state, then you do a count (implicitly grouping by day and state):
from django.db.models import Count
from django.db.models.functions import Trunc
queryset = (RegisterPage.c_month
.annotate(day=Trunc('detail_hour', 'day'))
.values('day', 'state')
.annotate(count=Count('day'))
.order_by('day', 'state')
)
I've added an ordering clause to remove any existing ordering (that would thwart the desired grouping) and to sort the results.
The results only include days and states that are actually present in the data, if you want to include missing days or states with the count 0
, you may want to do it in Python code.
You need a query with fields for day and state, then you do a count (implicitly grouping by day and state):
from django.db.models import Count
from django.db.models.functions import Trunc
queryset = (RegisterPage.c_month
.annotate(day=Trunc('detail_hour', 'day'))
.values('day', 'state')
.annotate(count=Count('day'))
.order_by('day', 'state')
)
I've added an ordering clause to remove any existing ordering (that would thwart the desired grouping) and to sort the results.
The results only include days and states that are actually present in the data, if you want to include missing days or states with the count 0
, you may want to do it in Python code.
answered Mar 24 at 23:20
Endre BothEndre Both
3,11611322
3,11611322
Yes want to include missing days or states with the count 0 how can i do it in python code ?
– Nathan Ingram
Mar 25 at 0:37
add a comment |
Yes want to include missing days or states with the count 0 how can i do it in python code ?
– Nathan Ingram
Mar 25 at 0:37
Yes want to include missing days or states with the count 0 how can i do it in python code ?
– Nathan Ingram
Mar 25 at 0:37
Yes want to include missing days or states with the count 0 how can i do it in python code ?
– Nathan Ingram
Mar 25 at 0:37
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%2f55329273%2fdjango-2-filter-models-by-every-day-of-current-month%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
It is not very clear what you are trying to do.
– Stargazer
Mar 24 at 22:56
Look March month have 31 days, I want to get number of every state of every day of march.
– Nathan Ingram
Mar 24 at 23:03