many-to-many

SQLAlchemy: filter by membership in at least one many-to-many related table

SQLAlchemy: filter by membership in at least one many-to-many related table Question: Using SQLAlchemy 0.7.1 and a MySQL 5.1 database, I’ve got a many-to-many relationship set up as follows: user_groups = Table(‘user_groups’, Base.metadata, Column(‘user_id’, String(128), ForeignKey(‘users.username’)), Column(‘group_id’, Integer, ForeignKey(‘groups.id’)) ) class ZKUser(Base, ZKTableAudit): __tablename__ = ‘users’ username = Column(String(128), primary_key=True) first_name = Column(String(512)) last_name = …

Total answers: 3

Django filter many-to-many with contains

Django filter many-to-many with contains Question: I am trying to filter a bunch of objects through a many-to-many relation. Because the trigger_roles field may contain multiple entries I tried the contains filter. But as that is designed to be used with strings I’m pretty much helpless how i should filter this relation (you can ignore …

Total answers: 4

Django ManyToManyField ordering using through

Django ManyToManyField ordering using through Question: Here is a snippet of how my models are setup: class Profile(models.Model): name = models.CharField(max_length=32) accout = models.ManyToManyField( ‘project.Account’, through=’project.ProfileAccount’ ) def __unicode__(self) return self.name class Accounts(models.Model): name = models.CharField(max_length=32) type = models.CharField(max_length=32) class Meta: ordering = (‘name’,) def __unicode__(self) return self.name class ProfileAccounts(models.Model): profile = models.ForeignKey(‘project.Profile’) account = …

Total answers: 7

How do I access the properties of a many-to-many "through" table from a django template?

How do I access the properties of a many-to-many "through" table from a django template? Question: From the Django documentation… When you’re only dealing with simple many-to-many relationships such as mixing and matching pizzas and toppings, a standard ManyToManyField is all you need. However, sometimes you may need to associate data with the relationship between …

Total answers: 3

What is related_name used for?

What is related_name used for? Question: What is the related_name argument useful for on ManyToManyField and ForeignKey fields? For example, given the following code, what is the effect of related_name=’maps’? class Map(db.Model): members = models.ManyToManyField(User, related_name=’maps’, verbose_name=_(‘members’)) Asked By: zjm1126 || Source Answers: The related_name attribute specifies the name of the reverse relation from the …

Total answers: 6

Get all table names in a Django app

Get all table names in a Django app Question: How to get all table names in a Django app? I use the following code but it doesn’t get the tables created by ManyToManyField from django.db.models import get_app, get_models app = get_app(app_name) for model in get_models(app): print model._meta.db_table Asked By: jack || Source Answers: The simplest …

Total answers: 4

Generic many-to-many relationships

Generic many-to-many relationships Question: I’m trying to create a messaging system where a message’s sender and recipients can be generic entities. This seems fine for the sender, where there is only object to reference (GenericForeignKey) but I can’t figure out how to go about this for the recipients (GenericManyToManyKey ??) Below is a simplified example. …

Total answers: 3