0

Django: 3.0.6

Below is my attempt to organize tags and tags for admin staff only (django-taggit is used).

from taggit.managers import TaggableManager # django-taggit
from taggit.models import TaggedItemBase # django-taggit

class SpecialAdminTaggedPost(TaggedItemBase):
    content_object = models.ForeignKey('Post',
                                       on_delete=models.PROTECT,
                                       related_name="admin_tagged_post")


class Post(models.Model):
    tags = TaggableManager() # django-taggit
    admin_tags = TaggableManager(through=SpecialAdminTaggedPost) # django-taggit. 

This gives the following error:

ERRORS:
post.Post.admin_tags: (fields.E304) Reverse accessor for 'Post.admin_tags' clashes with reverse accessor for 'Post.tags'.
    HINT: Add or change a related_name argument to the definition for 'Post.admin_tags' or 'Post.tags'.
post.Post.tags: (fields.E304) Reverse accessor for 'Post.tags' clashes with reverse accessor for 'Post.admin_tags'.
    HINT: Add or change a related_name argument to the definition for 'Post.tags' or 'Post.admin_tags'.
System check identified 2 issues (0 silenced).

Docs (not sure if it is of any help here): https://django-taggit.readthedocs.io/en/v0.10/custom_tagging.html

How can I cope with this problem?

halfer
  • 19,824
  • 17
  • 99
  • 186
Michael
  • 4,273
  • 3
  • 40
  • 69

2 Answers2

1

You need to use related_name in the model description as well. I ran into the same problem.

Your updated model like this would work:

class Post(models.Model):
    tags = TaggableManager(related_name='tags') 
    admin_tags = TaggableManager(related_name='admin_tags',
                                 through=SpecialAdminTaggedPost)  

I noticed that yes even if you use the related_name same as your model's field (i.e. tags and admin_tags in your case), it still works just fine.

Then you'd naturally go about accessing the tags for your post model:

<post_object>.tags 
# or 
<post_object>.admin_tags

All the APIs are listed here.

DaveIdito
  • 1,546
  • 14
  • 31
-2

I had the same problem when I mistakenly replaced models.py in one Django app by another models.py. So I had two exact copy of models.py in different apps. Pls check is everything ok with your models.py.