Skip to content

Instantly share code, notes, and snippets.

@teschmitt
Created June 26, 2018 13:10
Show Gist options
  • Save teschmitt/a9f7b6ef61bfc80f8571795b76516fc0 to your computer and use it in GitHub Desktop.
Save teschmitt/a9f7b6ef61bfc80f8571795b76516fc0 to your computer and use it in GitHub Desktop.
Test Inventory Database Models
class Product(models.Model):
profile = models.ForeignKey('accounts.Profile', null=True,
blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=180)
class ProductOption(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
nr = models.IntegerField(default=0)
name = models.CharField(max_length=20, null=False, blank=False)
def __str__(self):
return f'{self.product}: {self.name}'
class Meta:
unique_together = ('nr', 'product')
class ProductOptionValue(models.Model):
option = models.ForeignKey(ProductOption, on_delete=models.CASCADE)
value = models.CharField(max_length=20, null=False, blank=False)
def __str__(self):
return f'{self.option.name}: {self.value}'
class ProductSKUValue(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
option = models.ForeignKey(ProductOption, on_delete=models.CASCADE)
option_value = models.ForeignKey(ProductOptionValue, on_delete=models.CASCADE)
sku = models.CharField(max_length=40, null=False, blank=False, unique=True)
def __str__(self):
return f'{self.product.name} - {self.option_value}: {self.sku}'
class Meta:
unique_together = ('product', 'sku', 'option', 'option_value')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment