R
R
Robin Alikhanov2020-02-05 13:56:57
Django
Robin Alikhanov, 2020-02-05 13:56:57

How to import a model from another app in django?

Has project shop
in it apps blog, home
I am trying to get model from blog app

from django.shortcuts import render
from datetime import datetime
from django.http import HttpResponse

from shop.blog.models import Post


def get_home_page(request):
    # return HttpResponse("start")
    template = 'home/home.html'  # шаблон по умолчанию
    posts = Post.objects.filter(published_date__lte=datetime.now(), published=True)
    return render(request, template, {
        "posts": posts
    })


console throws an error now trying to get to the model.py of another app in a different way
ModuleNotFoundError: No module named 'shop.blog'

from django.shortcuts import render
from datetime import datetime
from django.http import HttpResponse

from ..blog.models import Post


def get_home_page(request):
    # return HttpResponse("start")
    template = 'home/home.html'  # шаблон по умолчанию
    posts = Post.objects.filter(published_date__lte=datetime.now(), published=True)
    return render(request, template, {
        "posts": posts
    })


console outputs
from ..blog.models import Post
ValueError: attempted relative import beyond top-level package


I don’t understand how to import a model from another application

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-02-05
@robin1985

from blog.models import Post

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question