alioguzhan
alioguzhan

Reputation: 7917

Django - Model field for user groups

I have model called PM to create Private Message objects. which is:

class Pm(models.Model):

    """
    Messaging System
    """
    subject = models.CharField(verbose_name='Message Subject',
                               max_length=50, blank=False)
    content = models.TextField(verbose_name='Message Content',
                               max_length=1000, blank=False)
    to = models.ForeignKey(settings.AUTH_USER_MODEL, null=False,
                           blank=False,verbose_name='Target',
                           related_name='target')
    sender = models.ForeignKey(settings.AUTH_USER_MODEL,
                               blank=False,verbose_name='Sender',
                               null=False,
                               related_name='source')

i can create a message to single user. If i change to field into a Many2Many field, then i can create this message for multiple users.

But what i want is to create this message for some user groups. There should be a group like Programmers which includes 10 users.

I am using Django Admin to manage this.

thanks

Upvotes: 1

Views: 4783

Answers (1)

yuvi
yuvi

Reputation: 18427

Just link the model to the Group object, like so:

from django.contrib.auth.models import Group

class Pm(models.Model):
    # ...
    group = models.ForeignKey(Group)

Small note: depending on your needs, it might be better to create your own User model with your own Group model. While these built-ins are great for out-of-the-box initialization, they're not very flexible, and are mostly aimed at managing the admin site.

Upvotes: 4

Related Questions