Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Getting hold of an instance of a model from inside RelatedManager [RSS Feed]

#1 Jan. 25, 2011 15:34:09

k.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Getting hold of an instance of a model from inside RelatedManager


I have some issues with getting hold of the instance of a Model to which a
RelatedManager got contributed to.
Is that at all possible and if so, how?

>From *my_method* below I can get hold of the model *RelatedStuff* by calling
self.model but that really doesn't get me there.

class MainStuff(models.Model):
....

class RelatedStuff(models.Model):
...
main = models.ForeignKey(MainStuff)
objects = CustomManager()

class CustomManager(models.Manager):
use_for_related_fields=True

def my_method(self):
#here I would like to access the instance of MainStuff to which this
got contributed


--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 25, 2011 15:56:05

Euan G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Getting hold of an instance of a model from inside RelatedManager


can't you do self.model.main?

On Jan 25, 3:34 pm, kmpm <pe...@birchroad.net> wrote:
> I have some issues with getting hold of the instance of a Model to which a
> RelatedManager got contributed to.
> Is that at all possible and if so, how?
>
> From *my_method* below I can get hold of the model *RelatedStuff* by calling
> self.model but that really doesn't get me there.
>
> class MainStuff(models.Model):
>     ....
>
> class RelatedStuff(models.Model):
>     ...
>     main = models.ForeignKey(MainStuff)
>     objects = CustomManager()
>
> class CustomManager(models.Manager):
>     use_for_related_fields=True
>
>     def my_method(self):
>         #here I would like to access the instance of MainStuff to which this
> got contributed

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 25, 2011 16:24:29

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Getting hold of an instance of a model from inside RelatedManager


On Tuesday, January 25, 2011 3:34:03 PM UTC, kmpm wrote:
>
> I have some issues with getting hold of the instance of a Model to which a
> RelatedManager got contributed to.
> Is that at all possible and if so, how?
>
> From *my_method* below I can get hold of the model *RelatedStuff* by
> calling self.model but that really doesn't get me there.
>
> class MainStuff(models.Model):
> ....
>
> class RelatedStuff(models.Model):
> ...
> main = models.ForeignKey(MainStuff)
> objects = CustomManager()
>
> class CustomManager(models.Manager):
> use_for_related_fields=True
>
> def my_method(self):
> #here I would like to access the instance of MainStuff to which
> this got contributed
>
>
>
Interesting question. Unfortunately, looking at the code, it doesn't seem
like you can. Django creates a new RelatedManager class every time, but it
does this via a closure - and the instance is only accessible within the
closure.

There are probably good reasons why it's done that way (I suspect passing
the instance as a parameter and making it available as an attribute would
cause a persistent reference to be kept, which would mean that the instance
would never be garbage-collected).
--
DR.

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 25, 2011 16:55:21

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Getting hold of an instance of a model from inside RelatedManager


On Tue, Jan 25, 2011 at 3:34 PM, kmpm <pe...@birchroad.net> wrote:
> I have some issues with getting hold of the instance of a Model to which a
> RelatedManager got contributed to.
> Is that at all possible and if so, how?
> From my_method below I can get hold of the model RelatedStuff by calling
> self.model but that really doesn't get me there.
> class MainStuff(models.Model):
>     ....
>
> class RelatedStuff(models.Model):
>     ...
>     main = models.ForeignKey(MainStuff)
>     objects = CustomManager()
> class CustomManager(models.Manager):
>     use_for_related_fields=True
>
>     def my_method(self):
>         #here I would like to access the instance of MainStuff to which this
> got contributed
>

What model instance?

I think you are discounting the fact that CustomManager instances may
be called without a related model instance. RelatedStuff.objects is an
instance of CustomManager, so RelatedStuff.objects.my_method() is a
valid method call, but what model instance do you think is relevant
there?

Cheers

Tom

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 25, 2011 17:46:35

k.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Getting hold of an instance of a model from inside RelatedManager


It's ok by me to return something generic or even raise an exception if I'm
calling directly using RelatedStuff.objects.my_method().

Is it possible for my_method to access MainStuff instance or at least find
out which way it is accessed?

Imagine I got a instance of MainStuff like this...
main = MainStuff.objects.get(id=1)

When I later call main.relatedstuff_set.my_method() I need "my_method" to
have access to "main" to do some additional filtering
and crunching depending on some of the fields in "main".

One other resort I possibly could do is some custom field instead, almost
like the reverse generic relation using
generic.GenericRelation. That would probably be a better pythonic/djangonic
way of doing it but fixing that would probably mean more codeing and less
progress for me.


--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Getting hold of an instance of a model from inside RelatedManager [RSS Feed]

Board footer

Moderator control

Enjoy the 11th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support