Speed up development with full-stack environments for every branch.

Learn More

Accessing Foreign Key Values [Python and Django]

749 Runs 6220 Views 632 Copies
Saved

Saved

bhawani0915 49

bhawani0915
published 3 years ago

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    def __unicode__(self):
        return self.name

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ForeignKey(Author)
    publisher = models.ForeignKey(Publisher)

    def __unicode__(self):
        return self.title
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).