Filtering generic relation with additional field

422 views
Skip to first unread message

guardbadger

unread,
Nov 4, 2011, 2:03:35 PM11/4/11
to Django users
Hi all,

Appreciate some help trying to build a queryset. Im hoping I can make
this in django, without having to crack open raw sql... Here is my
setup:

class Container(models.Model):
permissions = generic.GenericRelation(Permission)

class Item(models.Model):
container = models.ForeignKey(Container)
extraSetting = models.ForeignKey(ExtraSetting)

class Permission(models.Model):
content_type = models.ForeignKey(ContentType)
content_object = generic.GenericForeignKey()
extraSetting = models.ForeignKey(ExtraSetting)

class ExtraSetting(models.Model):
....

Convoluted made up example, but hopefully you can see what the data
might be.

So the problem im trying to solve. I need to get a list of items and
their permissions, but restrict the permission set so that I only get
permissions where extraSetting is the same on the Item and on the
permission.

So I want to run something like

Item.objects.filter(container__permissions__extraSetting=Item.extraSetting)

Hope this makes some form of sense...

thanks

guardbadger

Michal Petrucha

unread,
Nov 9, 2011, 4:03:05 AM11/9/11
to django...@googlegroups.com

What I think you want to use is this: http://django.me/f
In your case something like

Item.objects.filter(container__permissions__extraSetting=F('extraSetting'))

Michal

signature.asc

Anthony Musaluke

unread,
Nov 9, 2011, 6:41:08 AM11/9/11
to django...@googlegroups.com
Hi Michal

I a new to django but have been following the tutorial.
I have a question on your answer: Which file would you place this code in if you wanted to have that filtering visible in the site admin interface. Would you need to create a custom selection field from which you can pick to populate the relevant model field? I am not so sure yet how to tie that in since it looks like its using the DB access API at this stage?

Thanks

Anthony

kenneth gonsalves

unread,
Nov 9, 2011, 6:58:45 AM11/9/11
to django...@googlegroups.com
On Wed, 2011-11-09 at 13:41 +0200, Anthony Musaluke wrote:
> I a new to django but have been following the tutorial. I have a
> question on your answer: Which file would you place this code in if
> you wanted to have that filtering visible in the site admin interface.
> Would you need to create a custom selection field from which you can
> pick to populate the relevant model field? I am not so sure yet how to
> tie that in since it looks like its using the DB access API at this
> stage?

are you referring to this:
https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter

which is normally added to admin.py
--
regards
Kenneth Gonsalves

Anthony Musaluke

unread,
Nov 9, 2011, 7:33:16 AM11/9/11
to django...@googlegroups.com
Almost like that, except lets say you had a field you wanted to use as a list box but its contents depend on what you select in another field. Meaning, you want to use the results of a database query to populate it so you can select an item from the results.
Not sure if my explanation is making sense ;)

Thanks

Anthony


--
regards
Kenneth Gonsalves

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Reply all
Reply to author
Forward
0 new messages