Skip to content

Instantly share code, notes, and snippets.

@Joel-hanson
Last active June 3, 2019 04:13
Show Gist options
  • Save Joel-hanson/097d1a381011ba21f4dab23d83298eb5 to your computer and use it in GitHub Desktop.
Save Joel-hanson/097d1a381011ba21f4dab23d83298eb5 to your computer and use it in GitHub Desktop.
The example we are using to demonstrate dynamically modifying fields will have a Book model which has Publisher and Author as foreign and many to many relations.
from django.db import models
# Create your models here.
class Publisher(models.Model):
name = models.CharField(max_length=30, blank=False)
address = models.CharField(max_length=50, blank=False)
city = models.CharField(max_length=60, blank=False)
state_province = models.CharField(max_length=30, blank=False)
country = models.CharField(max_length=50, blank=False)
website = models.URLField()
def __unicode__(self):
return self.name
class Author(models.Model):
first_name = models.CharField(max_length=30, blank=False)
last_name = models.CharField(max_length=40, blank=False)
email = models.EmailField()
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
USD = 'USD'
CAD = 'CAD'
EUR = 'EUR'
AED = 'AED'
CURRENCY_CHOICES = [
(USD, 'US Dollar'),
(CAD, 'Canadian Dollar'),
(EUR, 'Euro'),
(AED, 'United Arab Emirates Dirham'),
]
title = models.CharField(max_length=100, blank=False)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
publication_date = models.DateField()
price = models.PositiveIntegerField()
currency = models.CharField(
max_length=3,
choices=CURRENCY_CHOICES,
default=USD,
)
def __unicode__(self):
return self.title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment