Unresolved reference 'models' - python

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?

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

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.

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

Related

Issues with Importing python files within a Django Project

I'm having problems with importing python files within my project. I've clearly set up a file named testing.py within a folder called api, which is the same directory where my views.py file. When I import testing within views.py, I keep getting an error: "ModuleNotFoundError: No module named 'testing'". I'm not sure if I need to create an init.py file here for the module, but it should be importing without any error regardless. Can anyone help me figure out this issue?
views.py file within api:
import http
from django.shortcuts import render
from django.shortcuts import HttpResponse
import testing
# Create your views here.
def test(request) :
return HttpResponse(testing.testFunction())
testing.py file within api:
def testFunction():
return "This is a test"
If it is in your API folder, it should be from api.testing import testFunction

importing models inside django application

I have made an app inside my django application. I'm trying to create a file(testabc.py) in the same directory as views.py and I want to import models in that file.
The name of model is "Example"
Now, in views.py, I import models in the following way:
from .models import Example
a = Example.objects.get()
Here, I am getting proper output
However, in my testabc.py file when I write the same code
I get the following error
from .models import Example
ValueError: Attempted relative import in non-package
You need to add file __init__.py in your folder.
Here is the doc for python module.

How can I use Django model externally from the app?

I have Django models Driver and Trip. Nothing in my views and no urls. I'm using Django as mere Database, using scripts to store stuff there in DB.
Here is my tree of how every thing looks:
loadmngr/
models.py
views.py
urls.py
management/
commands/
> it.py <
Assume I have init.py inside management and commands.
All I'm doing is a simple import of my Django models. Here is it.py:
import sys
parent = '/Users/work/TM/loadmngr'
sys.path.insert(0, parent)
from models import Trip
I run python manage.py it and I get a RunTimeError:
RunTimeError: Model class models.Driver doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded.
The latter part of the error ...or else was imported before its application was loaded is what I believe could be the problem.
Question is: How can I properly use my Django models externally with properly configured settings?

How can I use Flask Admin panel in a different package?

This is my application structure:
/blog
/blog
/app.py
models.py
views.py
/admin
__init__py
views.py
...
I want to use flask-admin extension in a different package.
in /admin/__init__.py I imported the app and flask-admin extension:
from flask.ext.admin import Admin
from app import app
then I initiate the admin app like that:
admin = Admin(app)
However, I get 404 error. Why? Should I use blueprint or what?
I assume you're trying to hit the default /admin routes within your Flask app for Flask admin?
My guess right now is that none of your code does import admin anywhere, which is probably good since admin's __init__.py will try to re-import your app.py all over again (from the from app import app reference) and you'll end up in a circular dependency.
What I'd do is alter app.py to contain the admin = Admin(app) and from flask.ext.admin import Admin code, and also do a from admin import views and empty out the admin/__init__.py file completely.

Could not import app.models in view file

First django app and am having a bit of trouble my project is laid out like this
MyProject
-dinners (python package, my app)
-views (python package)
__init__.py
dinners.py(conflict was here... why didn't I add this to the q.. sigh)
general.py
__init__.py
admin.py
models.py
tests.py
views.py
-Standard django project boilerplate
In my /views/general.py file it looks like this:
import os
import re
from django.http import HttpResponse
from django.template import Context,loader
from dinners.models import Dinner
def home(request):
first_page_dinners = Dinner.objects.all().order_by('-created_at')[:5]
t = loader.get_template('general/home.html')
c = Context({
'dinners':first_page_dinners,
})
return HttpResponse(t.render(Context()))
And in my urls.py file I have this regular expression to map to this view
url(r'^/*$','dinners.views.general.home', name="home")
However when I try to hit the home page I get this error:
Could not import dinners.views.general. Error was: No module named models
Removing the dinners.models import at the top of general.py (plus all of the model specific code) removes the error. Am I somehow importing incorrectly into the view file? Naturally I need to be able to access my models from within the view...
Thanks
UPDATE: answer I had a file dinners.py within the -views package that was conflicting
You need to put a __init__.py file in "dinners" and "views" folder to make those valid packages.
EDIT:
Also, remove that views.py file, that will create conflict with the package.

Categories