Add a GenericRelation in a Proxy Model

693 views Asked by At

I have a group of models in django 1.5.2 that all use GenericForeignKey and GenericRelation. The problem is that I also use proxy inheritance. A generic relation is a Field, but not a DB field, but when validating the model, django sees a Field in a proxy inherited model and raises an error.

Below is a simplified version of my problem. Is there a simple solution that would allow me to access simply the tags from TaggableArticle ?

from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType


class Tag(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')


class Article(models.Model):
    text = models.TextField()


class TaggableArticle(Article):
    tags = generic.GenericRelation(Tag)

    class Meta:
        proxy = True

The result is :

python manage.py validate            
FieldError: Proxy model 'TaggableArticle' contains model fields.
[1]    15784 exit 1     python manage.py validate

My thoughts so far :

  • django 1.6 introduces a for_concrete_model flag in generic relations, but I haven't been able to see if this could solve my problem. I just understood that it could help if the relation was in Article, and if I wanted to be able to tag an object with the proper content type (Article vs TaggedArticle).
  • I could remove the relation and just access it from the other endpoint (i.e. `Tag.objects.filter(content_object=myTaggedArticle) or manually add a method to this) but part of the implementation I have integrates the fact I'm using a RelatedManager and not just a normal Manager, and I would not be getting a related manager if I came from the whole TaggedArticle class.

Tkank you !

0

There are 0 answers