views:

24

answers:

2

I dam trying to do this: http://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name

Using this style

This is saved as common/abstract.py

class OtherModel(models.Model):

   something = Charfield(max_length=100)

   class Meta:
            abstract = True

class Base(models.Model):
        fk_model = models.ForeignKey(OtherModel, related_name="%(app_label)s_%(class)s_related")

        class Meta:
            abstract = True

Then I import it into another file. Let's call this app/models.py

from common.abstract import Base

class ChildB(Base):
    pass

I have installed 'app' but not the 'common'. It will import nicely without the FK or M2M relationship, but when I try to add it, I get this error:

/lib/python2.6/site-packages/django/db/models/fields/related.py", line 808, in init assert not to.meta.abstract, "%s cannot define a relation with abstract class %s" % (self.class._name__, to._meta.object_name) AssertionError: ForeignKey cannot define a relation with abstract class OtherModel

Please advise.... also let me know if you have any questions or don't understand something that I am explaining. The file that I working with is really complex, so I didn't want to post the whole thing since I knew that it is breaking on this one relationship.

A: 

You cannot have a relation with an abstract model, but this is what you are trying to do (ManyToManyField to OtherModel). If you want to make this work you need to remove abstract = True from OtherModel and add common to your INSTALLED_APPS!

If you want to relate to different sublasses of OtherModel from the subclasses of Base you will need to define the relation on the subclass, not on the abstract model!

lazerscience
That's what I thought, but what I don't understand is the docs give me the impression that I can have the relation. Am I wrong in the way I did it or are the docs wrong?
jakopanda
I do not know to which topic in the documentation you are referring exactly, but I do no think there would be any sense in defining a relation to an abstract model...
lazerscience
A: 

I figured out the problem.....

This will work:

from django.db import models

class CommonInfo(models.Model):

    name = models.CharField(max_length=100)

class Base(models.Model):
    m2m = models.ForeignKey(CommonInfo, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class ChildA(Base):
    pass

This will give you the error: AssertionError: ForeignKey cannot define a relation with abstract class OtherModel.This is because you can't have a FK relationship or M2M relationship on an abstract pointing to ANOTHER abstract class.

from django.db import models

class CommonInfo(models.Model):
    class Meta:
        abstract = True

    name = models.CharField(max_length=100)

class Base(models.Model):
    m2m = models.ForeignKey(CommonInfo, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class ChildA(Base):
    pass

This is sad news for me. Sad news indeed. I hope my sad news benefits someone else.

jakopanda