1

I'm getting the following error while running manage.py migrate in my Django rest-api. I'm unable to pinpoint where to look. Help appreciated.

attributeerror: 'str' object has no attribute '_meta'

The traceback shows the following :

'' Traceback (most recent call last): File "/home/myproject/myproject-api/manage.py", line 25, in execute_from_command_line(sys.argv)

File "/home/myEnv/lib/python3.9/site-packages/django/core/management/init.py", line 419, in execute_from_command_line utility.execute()

File "/home/myEnv/lib/python3.9/site-packages/django/core/management/init.py", line 395, in execute django.setup()

File "/home/myEnv/lib/python3.9/site-packages/django/init.py", line 24, in setup apps.populate(settings.INSTALLED_APPS)

File "/home/myEnv/lib/python3.9/site-packages/django/apps/registry.py", line 122, in populate app_config.ready()

File "/home/myEnv/lib/python3.9/site-packages/cacheops/init.py", line 18, in ready install_cacheops()

File "/home/myEnv/lib/python3.9/site-packages/funcy/flow.py", line 231, in wrapper return func(*args, **kwargs)

File "/home/myEnv/lib/python3.9/site-packages/cacheops/query.py", line 578, in install_cacheops opts = rel.through._meta AttributeError: 'str' object has no attribute '_meta'

''

6
  • Hi there, please consider adding the code here to get more responses You can check out the guidelines on how to ask a good question here <stackoverflow.com/help/how-to-ask> to get more responses with a potential answer. All the best Commented Sep 16, 2022 at 13:19
  • Hello there. I'm wondering which part of the code should i put since the stacktrace doesn't point to anything. Commented Sep 16, 2022 at 14:25
  • The issue here is that in your code, you provide a string object where another object is expected. Commented Sep 17, 2022 at 8:32
  • @Adrian: I am unable to pinpoint the location. Kindly check the temporary fix I figured as an answer. Waiting for your inputs. Commented Sep 17, 2022 at 15:35
  • @Earthling It seems that your configuration of cacheops may contain an error. The code stops in a setup function of cacheops. Hence, I recommend to check your configuration against the most up to date cacheops documentation. Commented Sep 18, 2022 at 6:23

2 Answers 2

0

The issue was this. I had to install

pip install django-cacheops==4.0.0

For the time being error is gone.

However, the original error stile persists if I install version django-cacheops==6.1.0

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

1 Comment

@Fidelis Maina, kindly review
0

The error you get from cacheops seems to have to do with the fact that you're using a many-to-many relationship with a through model, where you specify the through model by passing a string with the class's name instead of the class itself, because the class is only defined below. Although this is perfectly common practice, cacheops seems to require that you do it differently:

Namely, try to reference your through model by passing the class instead of a string with the class's name. For this, you need to reorder your code a bit, putting the class definition of your through model first, which of course requires you to pass strings instead of classes to ForeignKey there. E.g. do it like this:

class PostCategory(models.Model):
    post = models.ForeignKey('Post')
    category = models.ForeignKey('Category')

class Post(models.Model):
    pass

class Category(models.Model):
    posts = models.ManyToManyField(Post, through=PostCategory, related_name='categories')

1 Comment

Will try this out and post an update soon. Thank you.

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.