1đ
Groups are nothing but models
.
class Player(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()
Above, the Player
model
is for adding players. Now, if you want to group those players into teams, youâd do something like below:
class Team(models.Model):
players = models.ManyToManyField(Player)
team_name = models.CharField(max_length=100)
Now, you just have to create a team and keep adding players to it. Pretty easy, right?
Okay fine until now. Letâs say, there were 4 matches today which means 8 teams played those matches. Out of the total players who played today, 15 got injured, and 10 got banned from playing the next match. So if you want to make groups for injured and banned players, you would do something like below:
class Injured(models.Model):
players = models.ManyToManyField(Player)
class Banned(models.Model):
players = models.ManyToManyField(Player)
Thatâs it.
Now, the question is why would you use groups?
Of course, thereâs no need of writing group models, you have other options too.
class Player(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()
team = models.CharField(max_length=100)
is_injured = models.BooleanField(default=False)
is_banned = models.BooleanField(default=False)
Now think: 15 players were injured, which means you would have to update 15 playersâ is_injured
to True
. 8 were banned, which means youâd have to change 8 playersâ is_banned
to True
. Plus, youâd have to keep writing the name of every playerâs team again and again. It is really very time consuming. Now do you see the importance of groups?
Where does this code live?
In your models.py
file.
0đ
User groups are part of âdjango.contrib.authâ.
If you are using it with your user model (any normal user in Django 1.4 and lower, or custom user model WITH âPermissionsMixinâ in Django 1.5 or higher), then you will find respective tables in your database named:
- auth_group
- auth_group_permissions
- auth_permission
- auth_user_groups
- auth_user_user_permissions
Each user gets all the permissions set
- to groups (auth_groups) that are assigned to the user in the auth_user_groups table
- for the user in the auth_user_user_permissions
See django.contrib.aut for more information.
Adding user to groups was explained in this post
- [Answer]-Why is this template filter invalid?
- [Answer]-Celery Flower Is Able To Shutdown Worker But Unable To Restart It
- [Answer]-How to use ÄÄĆĄĆŸ in Django?
- [Answer]-Use ImagField from Django to save image paths in different directory other than MEDIA_ROOT