Re: Custom linking of models in many-to-one relationship

2009-09-27 Thread kmike

This looks like django's generic relations ('Content types' in
documentation).
`select_related` for generic related models is not supported in
django. So you should retreive all Notes associated with Account
manually. If there are several Accounts you still can fetch all data
in 2 queries using 'id__in' queries (example:
http://bitbucket.org/kmike/django-generic-images/src/tip/generic_utils/managers.py#cl-122).

On 28 сен, 09:17, triman  wrote:
> I have a model that can be attached to a variety of different models,
> based on the value of a column.  Is there any way to map this
> relationship in Django?  If not, is there a way to automatically do
> post-fetch processing where I could populate it?
>
> Here is a example:
>
> class Note(models.Model):
>     owner_id = models.IntegerField() #this is the FK of the related
> object
>     owner_type = models.CharField(max_length=40) # this can either be
> "Account" or "Ticket"
>     note = models.TextField()
>
> class Account(models.Model)
>     name = models.CharField(max_length=40)
>     ...
>
> class Ticket(models.Model)
>     subject = models.CharField(max_length=120)
>     ...
>
> So basically, we can attach Notes to either Account or Ticket.  The
> "owner_id" field will be the primary key of the object being attached
> to.  The "owner_type" will be either "Account" or "Ticket".  When
> retrieving an Account, I want to also return all the Notes associated
> with it.  I don't care about going the other direction in the
> relationship
>
> Is there any way to do this with the Django mappings?
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Custom linking of models in many-to-one relationship

2009-09-27 Thread triman

I have a model that can be attached to a variety of different models,
based on the value of a column.  Is there any way to map this
relationship in Django?  If not, is there a way to automatically do
post-fetch processing where I could populate it?

Here is a example:

class Note(models.Model):
owner_id = models.IntegerField() #this is the FK of the related
object
owner_type = models.CharField(max_length=40) # this can either be
"Account" or "Ticket"
note = models.TextField()

class Account(models.Model)
name = models.CharField(max_length=40)
...

class Ticket(models.Model)
subject = models.CharField(max_length=120)
...

So basically, we can attach Notes to either Account or Ticket.  The
"owner_id" field will be the primary key of the object being attached
to.  The "owner_type" will be either "Account" or "Ticket".  When
retrieving an Account, I want to also return all the Notes associated
with it.  I don't care about going the other direction in the
relationship

Is there any way to do this with the Django mappings?

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---