Opened 15 years ago
Closed 15 years ago
#13423 closed (invalid)
Subquerying using models
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.1 |
Severity: | Keywords: | models subquery | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using this models
class UserProfile(models.Model): data = models.ManyToManyField('self', through='DataPerms', symmetrical=False) .... class DataPerms (models.Model): user = models.ForeignKey(UserProfile) data = models.ForeignKey(Data) edit = models.BooleanField() view = models.BooleanField() .... class Data (models.Model): text = models.TextField(max_lenght=50)
One should be allowed to view all data a user can view. To do this i am currently doing something like
Data.objects.in_bulk(list(DataPerms.objects.filter(user = request.user, edit = True).values_list('data', flat=True))).values()
i think it should be a straight forward implementation to allow to fetch object fields from a QuerySet without having to iterate trough all. something like values() but that resolves PKs into objects and returns another QuerySet.
DataPerms.objects.filter(user = request.user, edit = True).field('data').all()
Should translate to
SELECT * FROM data
WHERE id in (SELECT id FROM dataperms
WHERE user = 'userid' && edit = 1)
Change History (2)
comment:1 by , 15 years ago
comment:2 by , 15 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
This is already possible using pkin:
Data.objects.filter(pk__in=DataPerms.objects.filter(user=request.user, edit=True).values('data'))
data = models.ManyToManyField('self', through='DataPerms', symmetrical=False)
should be
data = models.ManyToManyField(Data, through='DataPerms', symmetrical=False)