Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
Unresolved reference 'models'

I am writing custom template tag, and the error occurs that "Unresolved reference 'models'" the following is my blog_tags.py.

from django import template

from .models import Post

register = template.Library()


@register.simple_tag
def total_posts():
    return Post.published.count()

And my directory tree is as followed

blog/
  __init__.py 
  models.py 
  ...
  templatetags/
     __init__.py
     blog_tags.py

And i do have a Post class in my Models.

And when i click the prompt by pycharm "install package Post", after finishing installed it, the error disappear. I wonder do i have to do the same, which is install the package by the IDE, every time when i want to write a custom tag evolved with class in my Models?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

If I'm interpreting your project structure correctly, your models module is located in a parent package relative to blog_tags. Accessing .models would try to find the module inside your templatetags package.

Try to change your import to this instead:

from ..models import Post
over 4 years ago · Santiago Trujillo Relatório

0

As this is Django and as in Django circular imports can be an issue, consider dynamically loading the model:

for django 1.7+ use the application registry:

from django.apps import apps
Post = apps.get_model('blog', 'Post')

for earlier versions:

from django.db.models.loading import get_model
Post = get_model('blog', 'Post')

Note: This only works if 'blog' is an installed app.

over 4 years ago · Santiago Trujillo Relatório

0

import your models with app namespace instead of relative import, so that the standard structure is maintained.

from django import template

# blog is your app name
from blog.models import Post

register = template.Library()


@register.simple_tag
def total_posts():
    return Post.published.count()

Please check here unresolved error issue related to pycharm in django projects

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda