Re: How to get a queryset?
Thanks Max. In an ideal world left_connector, right_connector would be the way to go. However, I need to account for a cable that, at one or both ends is split into separate conductors, that go to separate connectors (thus breaking the rule I had specified.) Nodes, with a little embellishment, allow me to achieve this. I will investigate your itertools suggestion. Mike On Oct 20, 8:38 am, Max Battcherwrote: > adelaide_mike wrote: > > Hi > > My models are, essentially : > > class Cable(models.Model): > > cable = models.CharField(max_length=8) > > > class Connector(models.Model): > > connector = models.CharField(max_length=8) > > > class Node(models.Model): > > cable = models.ForeignKey(Cable) > > connector = models ForeignKey(Connector) > > > So, a real world cable can be plugged in to no, one, or two connectors > > (one at each end). The Node table describes the junctions between > > cables and connectors. > > > Can I, without resorting to raw SQL, build a queryset that shows > > connector-cable-connector? > > You would have a hard time even accomplishing that in raw SQL with those > models... If you have the flexibility to change your models I would try > something like: > > class Connector(models.Model): > name = models.CharField(max_length=8) > > class Cable(models.Model): > name = models.CharField(max_length-8) > left_connector = models.ForeignKey(Connector, blank=True, null=True) > right_connector = models.ForeignKey(Connector, blank=True, null=True) > > This makes your 0, 1, or 2 relationship explicit. I went with the > left/right distinction as it is an easy "handed" way of describing them, > but other choices may be more appropriate depending on your usage > patterns... > > Otherwise, your best bet would seem to me to be to use > Node.objects.all().orderby('cable') and group the > connector-cable-connector by yourself. Something that I generally find > useful in situations like that is Python's groupby function from itertools. > > -- > --Max Battcher--http://worldmaker.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 -~--~~~~--~~--~--~---
Re: How to get a queryset?
adelaide_mike wrote: > Hi > My models are, essentially : > class Cable(models.Model): > cable = models.CharField(max_length=8) > > class Connector(models.Model): > connector = models.CharField(max_length=8) > > class Node(models.Model): > cable = models.ForeignKey(Cable) > connector = models ForeignKey(Connector) > > So, a real world cable can be plugged in to no, one, or two connectors > (one at each end). The Node table describes the junctions between > cables and connectors. > > Can I, without resorting to raw SQL, build a queryset that shows > connector-cable-connector? You would have a hard time even accomplishing that in raw SQL with those models... If you have the flexibility to change your models I would try something like: class Connector(models.Model): name = models.CharField(max_length=8) class Cable(models.Model): name = models.CharField(max_length-8) left_connector = models.ForeignKey(Connector, blank=True, null=True) right_connector = models.ForeignKey(Connector, blank=True, null=True) This makes your 0, 1, or 2 relationship explicit. I went with the left/right distinction as it is an easy "handed" way of describing them, but other choices may be more appropriate depending on your usage patterns... Otherwise, your best bet would seem to me to be to use Node.objects.all().orderby('cable') and group the connector-cable-connector by yourself. Something that I generally find useful in situations like that is Python's groupby function from itertools. -- --Max Battcher-- http://worldmaker.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 -~--~~~~--~~--~--~---
How to get a queryset?
Hi My models are, essentially : class Cable(models.Model): cable = models.CharField(max_length=8) class Connector(models.Model): connector = models.CharField(max_length=8) class Node(models.Model): cable = models.ForeignKey(Cable) connector = models ForeignKey(Connector) So, a real world cable can be plugged in to no, one, or two connectors (one at each end). The Node table describes the junctions between cables and connectors. Can I, without resorting to raw SQL, build a queryset that shows connector-cable-connector? Thanks for any input. Mike --~--~-~--~~~---~--~~ 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: how to get a queryset by a ManyToMany field
This definitely makes sense, I have made a view called `books_by_author` - this made it easy for me to fit the logic into my feeble mind, and moreover it looks good in the URLconfs. Thanks a lot for suggestions! On Feb 8, 7:57 pm, "Jacob Kaplan-Moss" <[EMAIL PROTECTED]> wrote: > On 2/8/08, coldpizza <[EMAIL PROTECTED]> wrote: > > > And other related question: where it would be more logically correct > > to put this code? In URLConf as an parameter for the view, in a view > > of its own, or in a method bound to the Book model? > > I wouldn't put it in a URLconf. URLconfs are supposed to be > "configuration", so putting logic there seems dirty (to me). From > there, though, it's a bit of a matter of taste: do you consider "books > by an author" to be model logic, or view logic? > > For me, I'm usually guided by how I'll be using the query in question: > if it's designed to be accessed from non-view areas (template tags, > CLI scripts, etc.) I'll put it on the model, but if that particular > query is only used in some specific view, I'll leave it in the view. > Again, though, it's a matter of taste. > > Jacob --~--~-~--~~~---~--~~ 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: how to get a queryset by a ManyToMany field
On 2/8/08, coldpizza <[EMAIL PROTECTED]> wrote: > And other related question: where it would be more logically correct > to put this code? In URLConf as an parameter for the view, in a view > of its own, or in a method bound to the Book model? I wouldn't put it in a URLconf. URLconfs are supposed to be "configuration", so putting logic there seems dirty (to me). From there, though, it's a bit of a matter of taste: do you consider "books by an author" to be model logic, or view logic? For me, I'm usually guided by how I'll be using the query in question: if it's designed to be accessed from non-view areas (template tags, CLI scripts, etc.) I'll put it on the model, but if that particular query is only used in some specific view, I'll leave it in the view. Again, though, it's a matter of taste. Jacob --~--~-~--~~~---~--~~ 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: how to get a queryset by a ManyToMany field
Thanks a lot! Before posting I have spent over an hour reading and rereading that particular page in the doc section and in the Djangobook but still somehow the concept eluded me. I do admit though that I do not have previous experience solving this sort of issues in other languages. Maybe adding a real use case to the doc could make it easier to grasp. And other related question: where it would be more logically correct to put this code? In URLConf as an parameter for the view, in a view of its own, or in a method bound to the Book model? On Feb 8, 6:24 pm, "Jacob Kaplan-Moss" <[EMAIL PROTECTED]> wrote: > On 2/8/08, coldpizza <[EMAIL PROTECTED]> wrote: > > > Given that I have a Book model with a ManyToManyField called authors > > how do I get all the books for a particular author? Each book may have > > more than one author, so I cannot filter the books using the standard > > methods. > > You'll want to more closely read the documentation on related object > lookups:http://www.djangoproject.com/documentation/db-api/#related-objects. > > The short version, though, is this: > > Given a author ID, to find all the books by that author you'd use:: > > Book.objects.filter(authors__pk=author_id) > > Or, given a particular author object:: > > author.book_set.all() > > (Where did "book_set" come from? It's automatically created on the > "other side" of the many-to-many relation you created from Book to > Author. You can change the name of that relation attribute using the > "related_name" option to ManyToManyField). > > Jacob > > PS: Also see Chapter 5 of the Django Book > (http://djangobook.com/en/1.0/chapter05/);it's got examples pretty > similar to what you're doing. --~--~-~--~~~---~--~~ 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: how to get a queryset by a ManyToMany field
On 2/8/08, coldpizza <[EMAIL PROTECTED]> wrote: > Given that I have a Book model with a ManyToManyField called authors > how do I get all the books for a particular author? Each book may have > more than one author, so I cannot filter the books using the standard > methods. You'll want to more closely read the documentation on related object lookups: http://www.djangoproject.com/documentation/db-api/#related-objects. The short version, though, is this: Given a author ID, to find all the books by that author you'd use:: Book.objects.filter(authors__pk=author_id) Or, given a particular author object:: author.book_set.all() (Where did "book_set" come from? It's automatically created on the "other side" of the many-to-many relation you created from Book to Author. You can change the name of that relation attribute using the "related_name" option to ManyToManyField). Jacob PS: Also see Chapter 5 of the Django Book (http://djangobook.com/en/1.0/chapter05/); it's got examples pretty similar to what you're doing. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
how to get a queryset by a ManyToMany field
Sorry if this is too trivial but I have looked through the docs and could not find a clear answer. Given that I have a Book model with a ManyToManyField called authors how do I get all the books for a particular author? Each book may have more than one author, so I cannot filter the books using the standard methods. My models are: class Author(models.Model): name = models.CharField(max_length=124) class Book(models.Model): title = models.CharField(max_length=124) authors = models.ManyToManyField(Author) If I got it right, according to the documentation to get to a 'child' queryset I have to iterate with 'for each b in Book.objects.all()', and then call the b.authors.all() on each instance in order to check if a particular author.id is found in the authors queryset. But how do I get the results as a queryset? I could not get this to work. To get all the books for a given author.id, I have tried: def books_by_author(request, object_id): '''does not work''' filtered_by_author = [] for b in Book.objects.all(): if object_id in [a.id for a in b.authors.all()]: filtered_by_author.append(b) return render_to_response('polls/book_list.html', {'object_list': filtered_by_author,} ) But this, of course, does not work, because it returns a list and not a query set. What would be a proper, nice solution? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---