Proper way to add record to many to many relationship in Django
Posted
by
blcArmadillo
on Stack Overflow
See other posts from Stack Overflow
or by blcArmadillo
Published on 2010-12-31T23:47:52Z
Indexed on
2010/12/31
23:53 UTC
Read the original article
Hit count: 997
First off, I'm planning on running my project on google app engine so I'm using djangoappengine which as far as I know doesn't support django's ManyToManyField
type. Because of this I've setup my models like this:
from django.db import models
from django.contrib.auth.models import User
class Group(models.Model):
name = models.CharField(max_length=200)
class UserGroup(models.Model):
user = models.ForeignKey(User)
group = models.ForeignKey(Group)
On a page I have a form field where people can enter a group name. I want the results from this form field to create a UserGroup object for the user - group combination and if the group doesn't yet exist create a new Group object. At first I started putting this logic in the UserGroup class with a add_group
method but quickly realized that it doesn't really make sense to put this in the UserGroup
class. What would the proper way of doing this be? I saw some stuff about model managers. Is this what those are for?
© Stack Overflow or respective owner