Accessing super class methods in django
up vote
0
down vote
favorite
I have the following model,
a User
class User(AbstractBaseUser, PermissionsMixin, Base):
email = models.EmailField(db_index=True, unique=True, max_length=255)
mobile = PhoneNumberField(null=True)
username = models.CharField(db_index=True, null=False, unique=True, max_length=255)
A base class
class Room(Base):
name = models.CharField(db_index=True, unique=True, max_length=255)
status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
members = models.ManyToManyField(User)
last_activity = models.DateTimeField(default=timezone.now)
And two children
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
class ClubRoom(Room):
club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
I want to access the leagueroom_set
for the user. I'm not sure how to do this. The user currently has a property room_set
, but I wish to access the specific room_set. Can someone help me with this?
python django django-models
add a comment |
up vote
0
down vote
favorite
I have the following model,
a User
class User(AbstractBaseUser, PermissionsMixin, Base):
email = models.EmailField(db_index=True, unique=True, max_length=255)
mobile = PhoneNumberField(null=True)
username = models.CharField(db_index=True, null=False, unique=True, max_length=255)
A base class
class Room(Base):
name = models.CharField(db_index=True, unique=True, max_length=255)
status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
members = models.ManyToManyField(User)
last_activity = models.DateTimeField(default=timezone.now)
And two children
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
class ClubRoom(Room):
club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
I want to access the leagueroom_set
for the user. I'm not sure how to do this. The user currently has a property room_set
, but I wish to access the specific room_set. Can someone help me with this?
python django django-models
You are using multi-table inheritance here. Did you mean forRoom
to be a table in the database, or did you only want it to be a base class to define the shared fields for the league and club models?
– Martijn Pieters♦
Nov 11 at 17:18
@MartijnPieters I don't have any real use of the Room, but I can't make it abstract because I need the room id to globally sort all forms of room.
– Melissa Stewart
Nov 11 at 17:21
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following model,
a User
class User(AbstractBaseUser, PermissionsMixin, Base):
email = models.EmailField(db_index=True, unique=True, max_length=255)
mobile = PhoneNumberField(null=True)
username = models.CharField(db_index=True, null=False, unique=True, max_length=255)
A base class
class Room(Base):
name = models.CharField(db_index=True, unique=True, max_length=255)
status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
members = models.ManyToManyField(User)
last_activity = models.DateTimeField(default=timezone.now)
And two children
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
class ClubRoom(Room):
club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
I want to access the leagueroom_set
for the user. I'm not sure how to do this. The user currently has a property room_set
, but I wish to access the specific room_set. Can someone help me with this?
python django django-models
I have the following model,
a User
class User(AbstractBaseUser, PermissionsMixin, Base):
email = models.EmailField(db_index=True, unique=True, max_length=255)
mobile = PhoneNumberField(null=True)
username = models.CharField(db_index=True, null=False, unique=True, max_length=255)
A base class
class Room(Base):
name = models.CharField(db_index=True, unique=True, max_length=255)
status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
members = models.ManyToManyField(User)
last_activity = models.DateTimeField(default=timezone.now)
And two children
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
class ClubRoom(Room):
club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
I want to access the leagueroom_set
for the user. I'm not sure how to do this. The user currently has a property room_set
, but I wish to access the specific room_set. Can someone help me with this?
python django django-models
python django django-models
asked Nov 11 at 17:11
Melissa Stewart
780622
780622
You are using multi-table inheritance here. Did you mean forRoom
to be a table in the database, or did you only want it to be a base class to define the shared fields for the league and club models?
– Martijn Pieters♦
Nov 11 at 17:18
@MartijnPieters I don't have any real use of the Room, but I can't make it abstract because I need the room id to globally sort all forms of room.
– Melissa Stewart
Nov 11 at 17:21
add a comment |
You are using multi-table inheritance here. Did you mean forRoom
to be a table in the database, or did you only want it to be a base class to define the shared fields for the league and club models?
– Martijn Pieters♦
Nov 11 at 17:18
@MartijnPieters I don't have any real use of the Room, but I can't make it abstract because I need the room id to globally sort all forms of room.
– Melissa Stewart
Nov 11 at 17:21
You are using multi-table inheritance here. Did you mean for
Room
to be a table in the database, or did you only want it to be a base class to define the shared fields for the league and club models?– Martijn Pieters♦
Nov 11 at 17:18
You are using multi-table inheritance here. Did you mean for
Room
to be a table in the database, or did you only want it to be a base class to define the shared fields for the league and club models?– Martijn Pieters♦
Nov 11 at 17:18
@MartijnPieters I don't have any real use of the Room, but I can't make it abstract because I need the room id to globally sort all forms of room.
– Melissa Stewart
Nov 11 at 17:21
@MartijnPieters I don't have any real use of the Room, but I can't make it abstract because I need the room id to globally sort all forms of room.
– Melissa Stewart
Nov 11 at 17:21
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You could start from the other end:
league_rooms = LeagueRoom.objects.filter(members=my_user)
add a comment |
up vote
1
down vote
I think you want Room to be an abstract base class. Then what you are trying to do will work. https://docs.djangoproject.com/en/2.1/topics/db/models/#model-inheritance
class Room(models.Model):
name = models.CharField(db_index=True, unique=True, max_length=255)
# other fields
…
class Meta:
abstract = True
If Room can't be an abstract class, then you'll have to move your members field from Room into each of the subclasses to use the built-in relationships.
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
members = models.ManyToManyField(User)
# same with ClubRoom
Third option I can think of is to set a type attribute on each of your subclasses so that you can filter room_set to the type of room you want.
class LeagueRoom(Room):
…
type = models.CharField(max_length=10, default='league')
…
Then somewhere else:
room_set.filter(type='league')
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',
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%2f53251170%2faccessing-super-class-methods-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You could start from the other end:
league_rooms = LeagueRoom.objects.filter(members=my_user)
add a comment |
up vote
2
down vote
You could start from the other end:
league_rooms = LeagueRoom.objects.filter(members=my_user)
add a comment |
up vote
2
down vote
up vote
2
down vote
You could start from the other end:
league_rooms = LeagueRoom.objects.filter(members=my_user)
You could start from the other end:
league_rooms = LeagueRoom.objects.filter(members=my_user)
answered Nov 11 at 17:18
Daniel Roseman
442k41573628
442k41573628
add a comment |
add a comment |
up vote
1
down vote
I think you want Room to be an abstract base class. Then what you are trying to do will work. https://docs.djangoproject.com/en/2.1/topics/db/models/#model-inheritance
class Room(models.Model):
name = models.CharField(db_index=True, unique=True, max_length=255)
# other fields
…
class Meta:
abstract = True
If Room can't be an abstract class, then you'll have to move your members field from Room into each of the subclasses to use the built-in relationships.
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
members = models.ManyToManyField(User)
# same with ClubRoom
Third option I can think of is to set a type attribute on each of your subclasses so that you can filter room_set to the type of room you want.
class LeagueRoom(Room):
…
type = models.CharField(max_length=10, default='league')
…
Then somewhere else:
room_set.filter(type='league')
add a comment |
up vote
1
down vote
I think you want Room to be an abstract base class. Then what you are trying to do will work. https://docs.djangoproject.com/en/2.1/topics/db/models/#model-inheritance
class Room(models.Model):
name = models.CharField(db_index=True, unique=True, max_length=255)
# other fields
…
class Meta:
abstract = True
If Room can't be an abstract class, then you'll have to move your members field from Room into each of the subclasses to use the built-in relationships.
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
members = models.ManyToManyField(User)
# same with ClubRoom
Third option I can think of is to set a type attribute on each of your subclasses so that you can filter room_set to the type of room you want.
class LeagueRoom(Room):
…
type = models.CharField(max_length=10, default='league')
…
Then somewhere else:
room_set.filter(type='league')
add a comment |
up vote
1
down vote
up vote
1
down vote
I think you want Room to be an abstract base class. Then what you are trying to do will work. https://docs.djangoproject.com/en/2.1/topics/db/models/#model-inheritance
class Room(models.Model):
name = models.CharField(db_index=True, unique=True, max_length=255)
# other fields
…
class Meta:
abstract = True
If Room can't be an abstract class, then you'll have to move your members field from Room into each of the subclasses to use the built-in relationships.
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
members = models.ManyToManyField(User)
# same with ClubRoom
Third option I can think of is to set a type attribute on each of your subclasses so that you can filter room_set to the type of room you want.
class LeagueRoom(Room):
…
type = models.CharField(max_length=10, default='league')
…
Then somewhere else:
room_set.filter(type='league')
I think you want Room to be an abstract base class. Then what you are trying to do will work. https://docs.djangoproject.com/en/2.1/topics/db/models/#model-inheritance
class Room(models.Model):
name = models.CharField(db_index=True, unique=True, max_length=255)
# other fields
…
class Meta:
abstract = True
If Room can't be an abstract class, then you'll have to move your members field from Room into each of the subclasses to use the built-in relationships.
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
members = models.ManyToManyField(User)
# same with ClubRoom
Third option I can think of is to set a type attribute on each of your subclasses so that you can filter room_set to the type of room you want.
class LeagueRoom(Room):
…
type = models.CharField(max_length=10, default='league')
…
Then somewhere else:
room_set.filter(type='league')
edited Nov 12 at 1:29
answered Nov 12 at 1:23
Victor Bruno
886610
886610
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53251170%2faccessing-super-class-methods-in-django%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
You are using multi-table inheritance here. Did you mean for
Room
to be a table in the database, or did you only want it to be a base class to define the shared fields for the league and club models?– Martijn Pieters♦
Nov 11 at 17:18
@MartijnPieters I don't have any real use of the Room, but I can't make it abstract because I need the room id to globally sort all forms of room.
– Melissa Stewart
Nov 11 at 17:21