M2M with through model gives error fields.E336

241 views
Skip to first unread message

Colin Ellis

unread,
Sep 17, 2015, 2:01:56 PM9/17/15
to Django users
Greetings,

I'm trying to create a M2M relation between an abstract base class and a model that extends the same base.

I have the following code:

#Base object:
class BaseObject(models.Model):
    datetime = models.DateTimeField(auto_now_add=True)
    shared = models.ManyToManyField('userprofile.UserProfile',
                                through='models_bases.SharedObject',
                                symmetrical=False,
                                related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

<snip>

and the through table:

#Through table:
class SharedObject(models.Model):
    from userprofile.models import UserProfile

    class Meta:
        db_table = 'shared_object'
    userprofile = models.ForeignKey(UserProfile)
    content_type = models.ForeignKey(ContentType)
    object_pk = models.PositiveIntegerField()
    shared_object = GenericForeignKey(
                                ct_field="content_type",
                                fk_field="object_pk")
    datetime = models.DateTimeField(auto_now_add=True)


when I go to do ./manage.py makemigrations I get the following errors:

SystemCheckError: System check identified some issues:

ERRORS:
models_bases.SharedObject: (fields.E336) The model is used as an intermediate model by 'author.Author.shared', but it does not have a foreign key to 'Author' or 'UserProfile'.
models_bases.SharedObject: (fields.E336) The model is used as an intermediate model by 'award.Award.shared', but it does not have a foreign key to 'Award' or 'UserProfile'.
models_bases.SharedObject: (fields.E336) The model is used as an intermediate model by 'book.Book.shared', but it does not have a foreign key to 'Book' or 'UserProfile'.
models_bases.SharedObject: (fields.E336) The model is used as an intermediate model by 'publisher.Publisher.shared', but it does not have a foreign key to 'Publisher' or 'UserProfile'.
models_bases.SharedObject: (fields.E336) The model is used as an intermediate model by 'rlcomments.RLComment.shared', but it does not have a foreign key to 'RLComment' or 'UserProfile'.
models_bases.SharedObject: (fields.E336) The model is used as an intermediate model by 'statusposts.StatusPost.shared', but it does not have a foreign key to 'StatusPost' or 'UserProfile'.


The models are all extensions of BaseObject (including UserProfile).

I have found this ticket: https://code.djangoproject.com/ticket/11760#no1  which I believe to be related.

My question is how do i achieve a M2M with an abstract base that uses a through table?


Reply all
Reply to author
Forward
0 new messages