Help a Python newbie with a Django model inheritance problem

Posted by Joshmaker on Stack Overflow See other posts from Stack Overflow or by Joshmaker
Published on 2010-05-26T15:28:33Z Indexed on 2010/05/26 15:31 UTC
Read the original article Hit count: 456

I'm working on my first real Django project after years of PHP programming, and I am running into a problem with my models. First, I noticed that I was copying and pasting code between the models, and being a diligent OO programmer I decided to make a parent class that the other models could inherit from:

class Common(model.Model):
    self.name = models.CharField(max_length=255)
    date_created  = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.name

    class Meta:
        abstract=True

So far so good. Now all my other models extend "Common" and have names and dates like I want. However, I have a class for "Categories" were the name has to be unique. I assume there should be a relatively simple way for me to access the name attribute from Common and make it unique. However, the different methods I have tried to use have all failed. For example:

class Category(Common):
    def __init__(self, *args, **kwargs):
        self.name.unique=True

Spits up the error "Caught an exception while rendering: 'Category' object has no attribute 'name'

Can someone point me in the right direction?

© Stack Overflow or respective owner

Related posts about python

Related posts about django