Django - grouping a manytomany field sum
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Django 2.1, python 3.6
I have two models
house = (
('Clubs','Clubs'),
('Diamonds','Diamonds'),
('Hearts','Hearts'),
('Spades','Spades'),
)
class Card(models.Model):
id = models.CharField(max_length=36, blank=False, primary_key=True)
card = models.CharField(max_length=15, blank=False)
number = models.IntegerField(default=1)
house = models.CharField(choices=house)
class Deck(models.Model):
id = models.CharField(max_length=50, primary_key=True)
name = models.Charfield(...)
card = models.ManyToManyField(Card, through='DeckCard')
class DeckCard(models.Model):
deck = models.ForeeignKey(Deck, on_delete=models.cascade)
card = models.ForeignKey(Card, on_delete=models.CASCADE)
@property
def number_sum(self):
self._number_sum= self.card.aggregate(total=models.Sum('number'))['total']
return number_sum
The property field number_sum
successfully returns the correct count. However, I want to now group the sum by house
? Does anyone know how to do this?
I wasn't as clear as I should have been before and so I have expounded.
Where Card looks like it should
id...name........number...house
1....ace_diamonds..1......diamonds
2....two_diamonds..2......diamonds
3....three_diamonds..3....diamonds
4....four_diamonds...4....diamonds
...
52...king_Spades...13....spades
and Deck looks like
id...name
1...johnsdeck
2...bettysdeck
Suppose DeckCard looks like the following
deck..card
1...ace_diamonds
1...two_diamonds
1...ace_diamonds (yes there can be duplicates)
1...three_hearts
2...ace_hearts
Then the model Deck
is each person's set of cards. DeckCard
are the physical cards in the deck. and Card
contains the properties on the card.
I want to have Deck give some stats about the deck. E.g. total numbers per house.
So in the example I want to have a total count and a count per house
Deck 1
Total: 7 (1+2+1+3)
Diamonds: 4
Hearts: 3
Spades: 0
Clubs: 0
Deck 2
Total: 1
Hearts: 1
This
self._number_sum= self.card.aggregate(total=models.Sum('number'))
gives me a total count per deck, or in this case 7. However, I want to have a group by
so that I can have a count per house in addition to the total. Does anyone know how to do this.
In the end, I want to put this in a api like so
{
card: ...
numbers: {
Total:1
Diamonds:4
Clubs:0
Hearts:3
Spades:0
}
}
But that is not for this question. I'm pretty sure the best place to put this is on a @property
field. I'm just throwing that out there so someone knows the end goal.
django django-models
add a comment |
Django 2.1, python 3.6
I have two models
house = (
('Clubs','Clubs'),
('Diamonds','Diamonds'),
('Hearts','Hearts'),
('Spades','Spades'),
)
class Card(models.Model):
id = models.CharField(max_length=36, blank=False, primary_key=True)
card = models.CharField(max_length=15, blank=False)
number = models.IntegerField(default=1)
house = models.CharField(choices=house)
class Deck(models.Model):
id = models.CharField(max_length=50, primary_key=True)
name = models.Charfield(...)
card = models.ManyToManyField(Card, through='DeckCard')
class DeckCard(models.Model):
deck = models.ForeeignKey(Deck, on_delete=models.cascade)
card = models.ForeignKey(Card, on_delete=models.CASCADE)
@property
def number_sum(self):
self._number_sum= self.card.aggregate(total=models.Sum('number'))['total']
return number_sum
The property field number_sum
successfully returns the correct count. However, I want to now group the sum by house
? Does anyone know how to do this?
I wasn't as clear as I should have been before and so I have expounded.
Where Card looks like it should
id...name........number...house
1....ace_diamonds..1......diamonds
2....two_diamonds..2......diamonds
3....three_diamonds..3....diamonds
4....four_diamonds...4....diamonds
...
52...king_Spades...13....spades
and Deck looks like
id...name
1...johnsdeck
2...bettysdeck
Suppose DeckCard looks like the following
deck..card
1...ace_diamonds
1...two_diamonds
1...ace_diamonds (yes there can be duplicates)
1...three_hearts
2...ace_hearts
Then the model Deck
is each person's set of cards. DeckCard
are the physical cards in the deck. and Card
contains the properties on the card.
I want to have Deck give some stats about the deck. E.g. total numbers per house.
So in the example I want to have a total count and a count per house
Deck 1
Total: 7 (1+2+1+3)
Diamonds: 4
Hearts: 3
Spades: 0
Clubs: 0
Deck 2
Total: 1
Hearts: 1
This
self._number_sum= self.card.aggregate(total=models.Sum('number'))
gives me a total count per deck, or in this case 7. However, I want to have a group by
so that I can have a count per house in addition to the total. Does anyone know how to do this.
In the end, I want to put this in a api like so
{
card: ...
numbers: {
Total:1
Diamonds:4
Clubs:0
Hearts:3
Spades:0
}
}
But that is not for this question. I'm pretty sure the best place to put this is on a @property
field. I'm just throwing that out there so someone knows the end goal.
django django-models
Could you clarify what you want to achieve?C
is a charfield and what you can do is to count it, do you want to count how manyC
types?
– Sergey Pugach
Jan 4 at 9:01
I have completely updated my question, because it was too vague before.
– Micah Pearce
Jan 5 at 4:32
add a comment |
Django 2.1, python 3.6
I have two models
house = (
('Clubs','Clubs'),
('Diamonds','Diamonds'),
('Hearts','Hearts'),
('Spades','Spades'),
)
class Card(models.Model):
id = models.CharField(max_length=36, blank=False, primary_key=True)
card = models.CharField(max_length=15, blank=False)
number = models.IntegerField(default=1)
house = models.CharField(choices=house)
class Deck(models.Model):
id = models.CharField(max_length=50, primary_key=True)
name = models.Charfield(...)
card = models.ManyToManyField(Card, through='DeckCard')
class DeckCard(models.Model):
deck = models.ForeeignKey(Deck, on_delete=models.cascade)
card = models.ForeignKey(Card, on_delete=models.CASCADE)
@property
def number_sum(self):
self._number_sum= self.card.aggregate(total=models.Sum('number'))['total']
return number_sum
The property field number_sum
successfully returns the correct count. However, I want to now group the sum by house
? Does anyone know how to do this?
I wasn't as clear as I should have been before and so I have expounded.
Where Card looks like it should
id...name........number...house
1....ace_diamonds..1......diamonds
2....two_diamonds..2......diamonds
3....three_diamonds..3....diamonds
4....four_diamonds...4....diamonds
...
52...king_Spades...13....spades
and Deck looks like
id...name
1...johnsdeck
2...bettysdeck
Suppose DeckCard looks like the following
deck..card
1...ace_diamonds
1...two_diamonds
1...ace_diamonds (yes there can be duplicates)
1...three_hearts
2...ace_hearts
Then the model Deck
is each person's set of cards. DeckCard
are the physical cards in the deck. and Card
contains the properties on the card.
I want to have Deck give some stats about the deck. E.g. total numbers per house.
So in the example I want to have a total count and a count per house
Deck 1
Total: 7 (1+2+1+3)
Diamonds: 4
Hearts: 3
Spades: 0
Clubs: 0
Deck 2
Total: 1
Hearts: 1
This
self._number_sum= self.card.aggregate(total=models.Sum('number'))
gives me a total count per deck, or in this case 7. However, I want to have a group by
so that I can have a count per house in addition to the total. Does anyone know how to do this.
In the end, I want to put this in a api like so
{
card: ...
numbers: {
Total:1
Diamonds:4
Clubs:0
Hearts:3
Spades:0
}
}
But that is not for this question. I'm pretty sure the best place to put this is on a @property
field. I'm just throwing that out there so someone knows the end goal.
django django-models
Django 2.1, python 3.6
I have two models
house = (
('Clubs','Clubs'),
('Diamonds','Diamonds'),
('Hearts','Hearts'),
('Spades','Spades'),
)
class Card(models.Model):
id = models.CharField(max_length=36, blank=False, primary_key=True)
card = models.CharField(max_length=15, blank=False)
number = models.IntegerField(default=1)
house = models.CharField(choices=house)
class Deck(models.Model):
id = models.CharField(max_length=50, primary_key=True)
name = models.Charfield(...)
card = models.ManyToManyField(Card, through='DeckCard')
class DeckCard(models.Model):
deck = models.ForeeignKey(Deck, on_delete=models.cascade)
card = models.ForeignKey(Card, on_delete=models.CASCADE)
@property
def number_sum(self):
self._number_sum= self.card.aggregate(total=models.Sum('number'))['total']
return number_sum
The property field number_sum
successfully returns the correct count. However, I want to now group the sum by house
? Does anyone know how to do this?
I wasn't as clear as I should have been before and so I have expounded.
Where Card looks like it should
id...name........number...house
1....ace_diamonds..1......diamonds
2....two_diamonds..2......diamonds
3....three_diamonds..3....diamonds
4....four_diamonds...4....diamonds
...
52...king_Spades...13....spades
and Deck looks like
id...name
1...johnsdeck
2...bettysdeck
Suppose DeckCard looks like the following
deck..card
1...ace_diamonds
1...two_diamonds
1...ace_diamonds (yes there can be duplicates)
1...three_hearts
2...ace_hearts
Then the model Deck
is each person's set of cards. DeckCard
are the physical cards in the deck. and Card
contains the properties on the card.
I want to have Deck give some stats about the deck. E.g. total numbers per house.
So in the example I want to have a total count and a count per house
Deck 1
Total: 7 (1+2+1+3)
Diamonds: 4
Hearts: 3
Spades: 0
Clubs: 0
Deck 2
Total: 1
Hearts: 1
This
self._number_sum= self.card.aggregate(total=models.Sum('number'))
gives me a total count per deck, or in this case 7. However, I want to have a group by
so that I can have a count per house in addition to the total. Does anyone know how to do this.
In the end, I want to put this in a api like so
{
card: ...
numbers: {
Total:1
Diamonds:4
Clubs:0
Hearts:3
Spades:0
}
}
But that is not for this question. I'm pretty sure the best place to put this is on a @property
field. I'm just throwing that out there so someone knows the end goal.
django django-models
django django-models
edited Jan 5 at 4:32
Micah Pearce
asked Jan 4 at 6:24
Micah PearceMicah Pearce
470513
470513
Could you clarify what you want to achieve?C
is a charfield and what you can do is to count it, do you want to count how manyC
types?
– Sergey Pugach
Jan 4 at 9:01
I have completely updated my question, because it was too vague before.
– Micah Pearce
Jan 5 at 4:32
add a comment |
Could you clarify what you want to achieve?C
is a charfield and what you can do is to count it, do you want to count how manyC
types?
– Sergey Pugach
Jan 4 at 9:01
I have completely updated my question, because it was too vague before.
– Micah Pearce
Jan 5 at 4:32
Could you clarify what you want to achieve?
C
is a charfield and what you can do is to count it, do you want to count how many C
types?– Sergey Pugach
Jan 4 at 9:01
Could you clarify what you want to achieve?
C
is a charfield and what you can do is to count it, do you want to count how many C
types?– Sergey Pugach
Jan 4 at 9:01
I have completely updated my question, because it was too vague before.
– Micah Pearce
Jan 5 at 4:32
I have completely updated my question, because it was too vague before.
– Micah Pearce
Jan 5 at 4:32
add a comment |
0
active
oldest
votes
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%2f54033977%2fdjango-grouping-a-manytomany-field-sum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f54033977%2fdjango-grouping-a-manytomany-field-sum%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
Could you clarify what you want to achieve?
C
is a charfield and what you can do is to count it, do you want to count how manyC
types?– Sergey Pugach
Jan 4 at 9:01
I have completely updated my question, because it was too vague before.
– Micah Pearce
Jan 5 at 4:32