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]

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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