Reputation: 21
models.py
from django.db import models
from django.contrib.auth.models import User
class ActiveManager(models.Model):
def active(self):
return self.filter(active=True)
class Product(models.Model):
name = models.CharField(max_length=32)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits = 5, decimal_places = 2)
slug = models.SlugField(max_length=48)
active = models.BooleanField(default=True)
in_stock = models.BooleanField(default=True)
date_updated = models.DateTimeField(auto_now=True)
objects = ActiveManager()
tags = models.ManyToManyField(ProductTag, blank=True)
def __str__(self):
return self.name
class ProductImage(models.Model):
product = models.ForeignKey(
Product, on_delete=models.CASCADE)
image = models.ImageField(upload_to="product-images")
thumbnail = models.ImageField(
upload_to="product-thumbnails", null=True)
class ProductTag(models.Model):
name = models.CharField(max_length=40)
slug = models.SlugField(max_length=48)
description = models.TextField(blank=True)
active = models.BooleanField(default=True)
def __str__(self):
return self.name
def natural_key(self):
return(self.slug,)
The name error says ProductTag is not defined, whereas ProductTag itself a Class. I don't understand where I missed... appreciate for the help
Upvotes: 0
Views: 220
Reputation: 79
The solution must be like this, because I think reordering could solve your problem. However please put 2 lines of space between your classes and 1 line of space between your functions. It can make your code more readable and also more suitale for the Python guidelines.
from django.db import models
from django.contrib.auth.models import User
class ActiveManager(models.Model):
def active(self):
return self.filter(active=True)
class ProductTag(models.Model):
name = models.CharField(max_length=40)
slug = models.SlugField(max_length=48)
description = models.TextField(blank=True)
active = models.BooleanField(default=True)
def __str__(self):
return self.name
def natural_key(self):
return(self.slug,)
class Product(models.Model):
name = models.CharField(max_length=32)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits = 5, decimal_places = 2)
slug = models.SlugField(max_length=48)
active = models.BooleanField(default=True)
in_stock = models.BooleanField(default=True)
date_updated = models.DateTimeField(auto_now=True)
objects = ActiveManager()
tags = models.ManyToManyField(ProductTag, blank=True)
def __str__(self):
return self.name
class ProductImage(models.Model):
product = models.ForeignKey(
Product, on_delete=models.CASCADE)
image = models.ImageField(upload_to="product-images")
thumbnail = models.ImageField(
upload_to="product-thumbnails", null=True)
Upvotes: 1
Reputation: 4006
Looking at your code, you’re trying to use ProductTag before you’ve defined it.
You could either reorder things, or I believe you can reference by string and django will link them up later.
Eg tags = models.ManyToManyField('ProductTag', blank=True)
Upvotes: 0