1

Okay, I have been reading other django inheritance questions and I can't find anything to help. I may just have an understanding issue about how inheritance works. But here is my issue. To start, I have two base models that I'd like all of my other models to inherit from. Base Model just contains some useful methods for all of my models. The second is the start of an account specific object.

class BaseModel(models.Model):

# A couple of methods that all my models need to have. No fields. 

class AccountModel(models.Model):
    ''' A base model for items related to a specific account'''

    account = models.ForeignKey(Account)

    def save(self, request, *args, **kwargs):
        self.account = request.session['account']
        super(AccountModel, self).save(*args, **kwargs)

Then I have three models:

class Keyword(AccountModel) :
    keyword = models.CharField(max_length=300)
    #other fields, none required...

class Project(AccountModel) :
    project_name = models.CharField(max_length=200,verbose_name="Project Name")
    #other fields..

class KeywordTarget(BaseModel):
    keyword = models.ForeignKey(Keyword)
    url = models.URLField(null=True,blank=True)
    project = models.ForeignKey(Project)

But when I try to create a new Keyword, I get this error:

ValueError: Cannot assign "'something'": "Keyword.keyword" must be a "Keyword" instance.

when I do:

kw = Keyword(keyword = "something")

Where am I going wrong?

(Also, please don't tell me I should be using a ManyToMany through unless it solves the problem at hand)

2
  • you are probably doing something wrong, from the error it looks like that Keyword.keyword is a foreignkey not a CharField as defined in the code you pasted. Commented Mar 1, 2011 at 23:54
  • Yes... I am probably doing something wrong. It does look like that, but the code I posted is what is in there. Which is why I think it is an inheritance thing. Commented Mar 2, 2011 at 0:00

1 Answer 1

2

It looks like both BaseModel and Account model will be abstract, so you should specify that in the models' Meta objects like:

class BaseModel(models.Model):
    ...

    class Meta:
        abstract=True

(see http://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes)

I'm guessing that without that, you're ending up with interference between the inheriting models.

Sign up to request clarification or add additional context in comments.

3 Comments

@Michael - any suggestion for when BaseModel would be your custom django UserProfile? (This is running on 1.4, so the Userprofile changes in 1.5 are not considered.) Using abstract=True, I get back an issue with AUTH_PROFILE_MODULE not found.
@Chris - Do you mean specifying an abstract model as the AUTH_USER_PROFILE? I suspect that that just won't be possible--you might be able to do this if you were using muti-table inheritance, but suspect you'd be better off not complicating things when dealing with AUTH. Support for inheritance is somewhat weak in the core Django apps.
@MichaelC.O'Connor - Yep, we have a UserProfile class and 4 or 5 child classes that extend it for different types of profiles. (Students, teachers, etc.) However, UserProfile cannot be abstract if you use it for your AUTH_PROFILE_MODEL. I am looking forward to moving this app to 1.5 and seeing how the next User changes work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.