Consider

Book – Author

relationship.

————-

class Book(models.Model):

authors=models.ManyToManyField(Author)

—————

>>> b = Book.objects.get(id=50)

>>>b.authors.all()

this provides the list of authors

>>>b.authors.filter(name=’Lee’)

>>>a=Author.objects.get(name=’Lee’)

>>>a.book_set.all()

Similar to Foreign key value.

Foreign key is set in one-to-many relation.

For example,

Publisher – Book

—-

class Book(models.Model):

publisher = models.ForeignKey(Publisher)

—–

There are two cases:

>>> b = Book.objects.get(id=78)

>>>b.publisher

>>>p=Publisher.objects.get(name=’Aplus’)

>>>p.book_set.all()

Note that book_set

is just a QuerySet.

Thus it can be filtered and sliced.

>>>p.book_set.filter(name__icontains=’django’).order_by(‘name’)[0:2]

© 2011 Web Oom Suffusion theme by Sayontan Sinha
Switch to our mobile site
Top Footer