Reverse foreign key lookup with unknown model

2009-01-31 Thread Julian

hi there,

I have a problem I really cannot solve on my own. lets say I have
three models:

class A(models.Model):
 ...

class B(models.Model):
fk = models.OneToOneField("A")

class C(models.Model):
fk = models.OneToOneField("A")

If I have then a single A-instance, how can I determine to wich model
it is connected _without_ knowing wich models do have a OneToOneField
to A (or a ForeignKey to A)???

my instance

a = A()

has to methods in this case: a.b and a.c wich either return an
instance of B respectivly of C or throw an exception...but I have to
know their names to call them ;-)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Reverse foreign key lookup with unknown model

2009-01-31 Thread Antoni Aloy

2009/1/31 Julian :
>
> hi there,
>
> I have a problem I really cannot solve on my own. lets say I have
> three models:
>
> class A(models.Model):
> ...
>
> class B(models.Model):
>fk = models.OneToOneField("A")
>
> class C(models.Model):
>fk = models.OneToOneField("A")
>
> If I have then a single A-instance, how can I determine to wich model
> it is connected _without_ knowing wich models do have a OneToOneField
> to A (or a ForeignKey to A)???
>
> my instance
>
> a = A()
>

Just use a property in A

class A(models.Model):

   @property
   def connected(sefl):
   try:
 x = self.b
   except: # put the real exception here
  x = self.c
   return x

-- 
Antoni Aloy López
Blog: http://trespams.com
Site: http://apsl.net

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---