Re: one-to-many design question

2008-05-09 Thread Scott Moonen
Slight correction.  In your first example:

class Chapter(models.Model):
>book = models.ForeignKey(Chapter)
>

The parameter to ForeignKey should be book:

class Chapter(models.Model):
   book = models.ForeignKey(Book)

As mentioned earlier, you can locate a chapter's book as chapter.book, and
you can locate a book's chapters using book.chapter_set.all().

  -- Scott Moonen

On Fri, May 9, 2008 at 12:08 PM, Mike Chambers <[EMAIL PROTECTED]>
wrote:

>
> I am working on an app to host some books online. I cant figure out the
> best way to represent a one to many relationship.
>
> Basically, a Book has multiple Chapters, but a Chapter can only be in
> one book.
>
> Looking at the docs, this seems to be the way to represent this:
>
>
> --
> class Book(models.Model):
>
>
> class Chapter(models.Model):
>book = models.ForeignKey(Chapter)
> --
>
> Is that right? This seems a little counterintuitive to me, and something
> like this seems to make more sense:
>
>
> --
> class Book(models.Model):
>chapters = models.ManyToManyField(Chapter)
>
> class Chapter(models.Model):
> --
>
> Of course, that means that a chapter can be placed on multiple books.
>
> The second example, also seems to make it a little easier to work with
> Books, as I can do:
>
> Book.objects.all()
>
> and get the Chapters associated with each book.
>
> So, what is the "correct" way to model this relationship?
>
> Thanks for any input...
>
> mike
>
>
> >
>


-- 
http://scott.andstuff.org/  |  http://truthadorned.org/

--~--~-~--~~~---~--~~
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: one-to-many design question

2008-05-09 Thread Mike Chambers

Thanks for confirming.

Book.chapter_set.all()

gets all of the chapters for the book. Thanks!

mike

[EMAIL PROTECTED] wrote:
> The first way is the correct way, to get all the chapters for a given
> book you would do book_obj.chapter_set.all() , you can set what this
> attribute is named by doing ForeignKey(Chapter,
> related_name='this_thing').
> 
> On May 9, 11:08 am, Mike Chambers <[EMAIL PROTECTED]> wrote:
>> I am working on an app to host some books online. I cant figure out the
>> best way to represent a one to many relationship.
>>
>> Basically, a Book has multiple Chapters, but a Chapter can only be in
>> one book.
>>
>> Looking at the docs, this seems to be the way to represent this:
>>
>> --
>> class Book(models.Model):
>>
>> class Chapter(models.Model):
>> book = models.ForeignKey(Chapter)
>> --
>>
>> Is that right? This seems a little counterintuitive to me, and something
>> like this seems to make more sense:
>>
>> --
>> class Book(models.Model):
>> chapters = models.ManyToManyField(Chapter)
>>
>> class Chapter(models.Model):
>> --
>>
>> Of course, that means that a chapter can be placed on multiple books.
>>
>> The second example, also seems to make it a little easier to work with
>> Books, as I can do:
>>
>> Book.objects.all()
>>
>> and get the Chapters associated with each book.
>>
>> So, what is the "correct" way to model this relationship?
>>
>> 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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: one-to-many design question

2008-05-09 Thread [EMAIL PROTECTED]

The first way is the correct way, to get all the chapters for a given
book you would do book_obj.chapter_set.all() , you can set what this
attribute is named by doing ForeignKey(Chapter,
related_name='this_thing').

On May 9, 11:08 am, Mike Chambers <[EMAIL PROTECTED]> wrote:
> I am working on an app to host some books online. I cant figure out the
> best way to represent a one to many relationship.
>
> Basically, a Book has multiple Chapters, but a Chapter can only be in
> one book.
>
> Looking at the docs, this seems to be the way to represent this:
>
> --
> class Book(models.Model):
>
> class Chapter(models.Model):
>         book = models.ForeignKey(Chapter)
> --
>
> Is that right? This seems a little counterintuitive to me, and something
> like this seems to make more sense:
>
> --
> class Book(models.Model):
>         chapters = models.ManyToManyField(Chapter)
>
> class Chapter(models.Model):
> --
>
> Of course, that means that a chapter can be placed on multiple books.
>
> The second example, also seems to make it a little easier to work with
> Books, as I can do:
>
> Book.objects.all()
>
> and get the Chapters associated with each book.
>
> So, what is the "correct" way to model this relationship?
>
> 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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



one-to-many design question

2008-05-09 Thread Mike Chambers

I am working on an app to host some books online. I cant figure out the 
best way to represent a one to many relationship.

Basically, a Book has multiple Chapters, but a Chapter can only be in 
one book.

Looking at the docs, this seems to be the way to represent this:


--
class Book(models.Model):


class Chapter(models.Model):
book = models.ForeignKey(Chapter)
--

Is that right? This seems a little counterintuitive to me, and something 
like this seems to make more sense:


--
class Book(models.Model):
chapters = models.ManyToManyField(Chapter)

class Chapter(models.Model):
--

Of course, that means that a chapter can be placed on multiple books.

The second example, also seems to make it a little easier to work with 
Books, as I can do:

Book.objects.all()

and get the Chapters associated with each book.

So, what is the "correct" way to model this relationship?

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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---