Re: noob syntax question
very nice, thank you. I will look up the book. On Jun 24, 2:37 pm, Daniel Rosemanwrote: > On Jun 24, 7:01 pm, Sector7B wrote: > > > Thanks for the feedback and the link, all good stuff! > > I looked at what i had written, and I think a better way to ask the > > question is: > > What are the mechanics behind providing "choice_set.all()"? > > For example i have a field "choice" so having "_set" (concatenated) is > > that generated when i run syncdb somewhere, or is that dynamically > > interpreted at runtime? > > My background leads me to think there would be a choice.set().all(), > > the "_set" seems weird (to me) unless its generated. > > > Thanks again, > > -j > > It's dynamically generated when the model class is defined. Fields can > define a 'contribute_to_class' method, which is responsible for > defining dynamic properties/methods on the model class. When the field > is instantiated - which happens when the class is defined, ie when its > module is first imported - the field's contribute_to_class method is > executed and the related manager is created. You can see the code for > this in django.db.models.fields.related.RelatedField. (Actually you'll > see that contribute_to_class calls do_related_class, which calls > contribute_to_related_class, which sets up the descriptor on the > related model.) > > A very good guide to all this is Marty Alchin's book Pro Django, which > goes into depth as to how the model metaclasses work. > -- > 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-us...@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: noob syntax question
On Jun 24, 7:01 pm, Sector7Bwrote: > Thanks for the feedback and the link, all good stuff! > I looked at what i had written, and I think a better way to ask the > question is: > What are the mechanics behind providing "choice_set.all()"? > For example i have a field "choice" so having "_set" (concatenated) is > that generated when i run syncdb somewhere, or is that dynamically > interpreted at runtime? > My background leads me to think there would be a choice.set().all(), > the "_set" seems weird (to me) unless its generated. > > Thanks again, > -j > It's dynamically generated when the model class is defined. Fields can define a 'contribute_to_class' method, which is responsible for defining dynamic properties/methods on the model class. When the field is instantiated - which happens when the class is defined, ie when its module is first imported - the field's contribute_to_class method is executed and the related manager is created. You can see the code for this in django.db.models.fields.related.RelatedField. (Actually you'll see that contribute_to_class calls do_related_class, which calls contribute_to_related_class, which sets up the descriptor on the related model.) A very good guide to all this is Marty Alchin's book Pro Django, which goes into depth as to how the model metaclasses work. -- 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-us...@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: noob syntax question
Thanks for the feedback and the link, all good stuff! I looked at what i had written, and I think a better way to ask the question is: What are the mechanics behind providing "choice_set.all()"? For example i have a field "choice" so having "_set" (concatenated) is that generated when i run syncdb somewhere, or is that dynamically interpreted at runtime? My background leads me to think there would be a choice.set().all(), the "_set" seems weird (to me) unless its generated. Thanks again, -j On Jun 24, 3:09 am, "euan.godd...@googlemail.com"wrote: > I'd add to Michael's comment that if you're unhappy with this syntax > (I personally find it a bit odd in some cases), you can customize > exactly what word is used there in your model definition. > > If you alter the choice model and add the "related_name" keyword to > the foreign key definition, e.g.: > > class Choice(models.Model): > ... > poll = models.ForeignKey(Poll, related_name="choices") > > you can then do: > > >>> p.choices.create(...) > > On 23 June, 23:17, Michael Schade wrote: > > > > > (Sorry in advance for the brevity and any typos, I am typing this from > > my aging Windows Mobile). > > > It's quite Django-specific actually. If you take a look > > athttp://docs.djangoproject.com/en/dev/topics/db/queries/ it says, > > "Django also creates API accessors for the "other" side of the > > relationship -- the link from the related model to the model that > > defines the relationship. For example, a Blog object b has access to a > > list of all related Entry objects via the entry_set attribute: > > b.entry_set.all()." > > > Hope that clears things up. > > > Michael Schade > > Spearhead Development LLC > > > On 6/23/10, Sector7B wrote: > > > > Hi, > > > In the tutorial 1. > > > > It has this: > > > # Give the Poll a couple of Choices. The create call constructs a new > > > # choice object, does the INSERT statement, adds the choice to the set > > > # of available choices and returns the new Choice object. Django > > > creates > > > # a set to hold the "other side" of a ForeignKey relation > > > # (e.g. a poll's choices) which can be accessed via the API. > > > > and gives these examples: > > > # Create three choices. > > p.choice_set.create(choice='Not much', votes=0) > > > > > p.choice_set.create(choice='The sky', votes=0) > > > > > c = p.choice_set.create(choice='Just hacking again', votes=0) > > > > I understand what its doing, but I don't understand where the "_set" > > > comes from or where its resolved to. > > > Its probably more of a python thing than a django thing, but if > > > someone could provide insight, it would be much appreciate. > > > > Thanks, > > > -j > > > > -- > > > You received this message because you are subscribed to the Google Groups > > > "Django users" group. > > > To post to this group, send email to django-us...@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. > > > -- > > Sincerely, > > Michael Schadewww.mschade.me-815.514.1410 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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: noob syntax question
I'd add to Michael's comment that if you're unhappy with this syntax (I personally find it a bit odd in some cases), you can customize exactly what word is used there in your model definition. If you alter the choice model and add the "related_name" keyword to the foreign key definition, e.g.: class Choice(models.Model): ... poll = models.ForeignKey(Poll, related_name="choices") you can then do: >>> p.choices.create(...) On 23 June, 23:17, Michael Schadewrote: > (Sorry in advance for the brevity and any typos, I am typing this from > my aging Windows Mobile). > > It's quite Django-specific actually. If you take a look > athttp://docs.djangoproject.com/en/dev/topics/db/queries/ it says, > "Django also creates API accessors for the "other" side of the > relationship -- the link from the related model to the model that > defines the relationship. For example, a Blog object b has access to a > list of all related Entry objects via the entry_set attribute: > b.entry_set.all()." > > Hope that clears things up. > > Michael Schade > Spearhead Development LLC > > On 6/23/10, Sector7B wrote: > > > > > > > Hi, > > In the tutorial 1. > > > It has this: > > # Give the Poll a couple of Choices. The create call constructs a new > > # choice object, does the INSERT statement, adds the choice to the set > > # of available choices and returns the new Choice object. Django > > creates > > # a set to hold the "other side" of a ForeignKey relation > > # (e.g. a poll's choices) which can be accessed via the API. > > > and gives these examples: > > # Create three choices. > p.choice_set.create(choice='Not much', votes=0) > > > p.choice_set.create(choice='The sky', votes=0) > > > c = p.choice_set.create(choice='Just hacking again', votes=0) > > > I understand what its doing, but I don't understand where the "_set" > > comes from or where its resolved to. > > Its probably more of a python thing than a django thing, but if > > someone could provide insight, it would be much appreciate. > > > Thanks, > > -j > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@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. > > -- > Sincerely, > Michael Schadewww.mschade.me- 815.514.1410 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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: noob syntax question
(Sorry in advance for the brevity and any typos, I am typing this from my aging Windows Mobile). It's quite Django-specific actually. If you take a look at http://docs.djangoproject.com/en/dev/topics/db/queries/ it says, "Django also creates API accessors for the "other" side of the relationship -- the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entry objects via the entry_set attribute: b.entry_set.all()." Hope that clears things up. Michael Schade Spearhead Development LLC On 6/23/10, Sector7Bwrote: > Hi, > In the tutorial 1. > > It has this: > # Give the Poll a couple of Choices. The create call constructs a new > # choice object, does the INSERT statement, adds the choice to the set > # of available choices and returns the new Choice object. Django > creates > # a set to hold the "other side" of a ForeignKey relation > # (e.g. a poll's choices) which can be accessed via the API. > > and gives these examples: > # Create three choices. p.choice_set.create(choice='Not much', votes=0) > p.choice_set.create(choice='The sky', votes=0) > c = p.choice_set.create(choice='Just hacking again', votes=0) > > I understand what its doing, but I don't understand where the "_set" > comes from or where its resolved to. > Its probably more of a python thing than a django thing, but if > someone could provide insight, it would be much appreciate. > > Thanks, > -j > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@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. > > -- Sincerely, Michael Schade www.mschade.me - 815.514.1410 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
noob syntax question
Hi, In the tutorial 1. It has this: # Give the Poll a couple of Choices. The create call constructs a new # choice object, does the INSERT statement, adds the choice to the set # of available choices and returns the new Choice object. Django creates # a set to hold the "other side" of a ForeignKey relation # (e.g. a poll's choices) which can be accessed via the API. and gives these examples: # Create three choices. >>> p.choice_set.create(choice='Not much', votes=0) >>> p.choice_set.create(choice='The sky', votes=0) >>> c = p.choice_set.create(choice='Just hacking again', votes=0) I understand what its doing, but I don't understand where the "_set" comes from or where its resolved to. Its probably more of a python thing than a django thing, but if someone could provide insight, it would be much appreciate. Thanks, -j -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.