I'm trying to get a count of the number of Users
that are connected to a Room
. A User
can be connected to only one Room
, a Room
can have many Users
.
My models look like this
class User(AbstractBaseUser, PermissionsMixin):
"""Database model for users"""
screen_name = models.CharField(max_length=255, unique=True)
room = models.ForeignKey(
Room, related_name='room', on_delete=models.CASCADE
)
...
class Room(models.Model):
"""Database model for rooms"""
name = models.CharField(max_length=100)
is_full = models.BooleanField(default=False)
...
What I'm trying to do is lock any instance of a Room
out once a certain number of User
s have been assigned to it. How do I go about finding the number of User
s connected to a given room? Pseudo code of what I'm trying.
my_room.users.count >= max_users:
my_room.is_full = True
Thank you in advance.
question from:
https://stackoverflow.com/questions/65889605/django-foregin-key-count 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…