One-to-many relations

2007-07-05 Thread mrstone

Hi all

I have a problem I have been struggeling with for a while.

I have two models:

InfoProxy and Permission

A InfoProxy can have several Permissions

class Permission(models.Model):
 info_proxy = models.ForeignKey(InfoProxy,
related_name='permissions')
 permission_type = models.CharField(maxlength=1,
choices=PERMISSION_CHOICES)
 usergroup =   models.ForeignKey(UserGroup)

class InfoProxy(models.Model, UrlItem):
"""
   A proxy pointing to a (Info)Item
"""
info_type = models.ForeignKey(ContentType,
related_name='infoproxies')
info_id = models.PositiveIntegerField()
info_item = generic.GenericForeignKey(ct_field="info_type",
fk_field="info_id")

render_type = models.ForeignKey(ContentType,
related_name='render_infoproxies', blank=True, null=True)
render_id = models.PositiveIntegerField(blank=True, null=True)
render_item = generic.GenericForeignKey(ct_field="render_type",
fk_field="render_id")

created = models.DateTimeField('date created')
modified = models.DateTimeField('date modified')
owner = models.ForeignKey(User)

language_code = models.CharField(maxlength=20)
rank = models.IntegerField( blank=True, null=True)
grade = models.IntegerField( blank=True, null=True)
objects = InfoProxyManager()

I want to query for all object a user has permission to read depending
on groups (code below)
This code works without any problems on developer server. On
mod_python however I (sometimes) get the below error.
If I hit refresh in the browser, it usually works.

I have tried several things like:
All imports are with full paths
Classes put in site-packages
(Maybe something is wrong when loading classes)
I have checked so at least one permission exist on every InfoProxy

Anyone have had a similar problem?
Any hint that can help me try other options?

Any help is very much appreciated.
Sten


--
Error trace
--
  File "/usr/local/lib/python2.4/site-packages/django/core/handlers/
base.py", line 77, in get_response
response = callback(request, *callback_args, **callback_kwargs)

  File "/usr/local/lib/python2.4/site-packages/jellyspot/server/apps/
locations/proxy/views.py", line 264, in flashes
result_list = ResultList(request, query)

  File "/usr/local/lib/python2.4/site-packages/jellyspot/server/apps/
common/results.py", line 36, in __init__
self.get_results(request)

  File "/usr/local/lib/python2.4/site-packages/jellyspot/server/apps/
common/results.py", line 84, in get_results
raise IncorrectLookupParameters(str(e))

IncorrectLookupParameters: Cannot resolve keyword 'permissions' into
field. Choices are: id, info_type, info_id, render_type, render_id,
created, modified, owner, indexer, language_code, rank, grade


query code

try:
if user and user.is_authenticated():

query = """select uub.usergroup_id  from users_buddy
as ub
left join users_usergroup_buddies as uub on
uub.buddy_id = ub.id  where ub.buddy_id = %s
""" % (user.id)

cursor = connection.cursor()
cursor.execute(query)

group_idents = [item[0] for item in cursor.fetchall()]
group_idents.append(USERGROUP_MEMBERS.id)
else:
group_idents = []

group_idents.append(USERGROUP_EVERYONE.id)


if user and user.is_authenticated():
 qs = self.all().filter( Q(owner__exact=user.id) |
 
Q(permissions__usergroup__in=group_idents)).distinct()
else:
 qs =
self.all().filter(permissions__usergroup__in=group_idents).distinct()

return qs
except Exception, e:
print e
return self.get_empty_query_set()


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: get many from multiple levels of one-to-many relations

2006-07-19 Thread Corey Oordt

Thanks, Andy. I'll give that a try!

Corey

On Jul 18, 2006, at 9:35 PM, Andrew wrote:

>
> Here's how I did it for a similar situation I had.
>
> class MyCategory(models.Model):
> category = models.CharField(maxlength=50)
> parent_category = models.ForeignKey('self', blank=True,
> null=True, related_name='sub_categories')
>
> def all_items(self):
> data = list(self.items.all())
> for cat in self.sub_categories.all():
> data.append(list(cat.all_items()))
> return data
>
> class MyItem(models.Model):
> name = models.CharField(maxlength=100)
> categories = models.ManyToManyField(MyCategory,
> related_name='items')
>
> This is not the most effecient way of doing things since the list
> command forces the queryset to evaluate completely.  Perhaps we should
> consider adding a UNION function to combine QuerySets if it's not
> already in there.
>
> Andy
>
>
> >


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



Re: get many from multiple levels of one-to-many relations

2006-07-18 Thread Andrew

Here's how I did it for a similar situation I had.

class MyCategory(models.Model):
category = models.CharField(maxlength=50)
parent_category = models.ForeignKey('self', blank=True,
null=True, related_name='sub_categories')

def all_items(self):
data = list(self.items.all())
for cat in self.sub_categories.all():
data.append(list(cat.all_items()))
return data

class MyItem(models.Model):
name = models.CharField(maxlength=100)
categories = models.ManyToManyField(MyCategory,
related_name='items')

This is not the most effecient way of doing things since the list
command forces the queryset to evaluate completely.  Perhaps we should
consider adding a UNION function to combine QuerySets if it's not
already in there.

Andy


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



get many from multiple levels of one-to-many relations

2006-07-18 Thread Corey

I have three tables: pubs, routes, and addresses, there is a
hierarchical relation ship between them:

one pub has many routes which in turn has many addresses.

Simplified models:

class Publication(models.Model):
code = models.CharField(blank=True, maxlength=10, unique=True)
name = models.CharField(blank=False, maxlength=30)

class Route(models.Model):
zipcode_crrt = models.CharField(blank=False, maxlength=9,
primary_key=True)
zipcode = models.ForeignKey(ZIP_Code)
carrier_rt = models.CharField(blank=False, null=False, maxlength=4,
db_index=True)
publication = models.ForeignKey(Publication, null=True)

class Address(models.Model):
route = models.ForeignKey(Route)
delivery_seq = models.IntegerField(blank=False, null=False)


Given a publication object, how can I get all the Addresses that belong
to it?

Thanks for the help!

Corey


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