I can think of a couple of ways:
1. Using intermediate table for Many-to-many relationship:
class UserPermission(models.Model):
user = models.ForeignKey(User)
permission = models.ForeignKey(Permission)
user_permissions = UserPermission.objects.select_related().all()
2. Writing some custom SQL query to select the same thing -- the
intermediate table, joining users and permissions, and then ordering
by users.
Regards,
Aidas Bendoraitis aka Archatas
On 1/3/07, Trey <[EMAIL PROTECTED]> wrote:
Is there a problem in django's many-to-many relationship mapper when it
comes to selecting the related fields? In this simple example I have a
user with permissions and they are related via an intermediary table.
"select_related()" only brings back the User object and each access of
a Permission object results in a query.
Is there a way to avoid making N queries to the database where N is the
number of users that I have?
Example Model and ORM Call:
class App(models.Model):
name = models.CharField(maxlength=32)
class Permission(models.Model):
name = models.CharField(maxlength=32)
description = models.CharField(maxlength=255)
app = models.ForeignKey(App)
class User(models.Model):
username = models.CharField(maxlength=32)
password = models.CharField(maxlength=48)
first_name = models.CharField(maxlength=50)
last_name = models.CharField(maxlength=50)
permissions = models.ManyToManyField('Permission')
user = User.objects.select_related().all()
Thanks, Trey
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---