[Django]-Proper way to add record to many to many relationship in Django

3👍

From the Django docs:

A Manager is the interface through which database query operations are provided to Django models

So a manager is not the way to create new instances of a model.

I would just have a constructor for the Group model which pulls the User (if it is given) from the keyword arguments and creates a new UserGroup.

So if you instantiated a new Group as Group(name='group name', user=some_user) the constructor could strip the user keyword argument away and create the appropriate UserGroup with that user:

class Group(models.Model):
    name = models.CharField(max_length=200)

    def __init__(self, *args, **kwargs):
        # Remove user from the kwargs.
        # It is important that user is removed from the kwargs before calling
        # the super.__init__(). This is because the `user` kwarg is not expected.
        user = kwargs.pop('user', None)

        # call the normal init method
        super(Group, self).__init__(*args, **kwargs)

        # Create the UserGroup instance for the specified user
        if user is not None:
            # Maybe some other logic here ...
            UserGroup(user=user, group=self).save()

This way if you provide a user when instantiating your Group it will create a UserGroup and will do nothing otherwise.

Of course you could equally do a similar thing in the constructor of the UserGroup model, it is not really that important, it just depends which metaphorically makes sense to your code.

EDIT:

A fix to the problems pointed out in the comments:

...
def __init__(self, *args, **kwargs):
    ...
    self._user_group = UserGroup(user=user, group=self)

def save(self, *args, **kwargs):
    super(Group, self).save(*args, **kwargs)
    self._user_group.save()

Leave a comment