Import Django models from outside the project - django

I'm working on an app which uses two tables from different databases.I manage to make the connection and make the tables structures in models.py, but now one I change the models.py file, I copy one of the tables in another python script, and I put the file elsewhere for other people to use it.My question it is possible in Django to import a model from outside the project? or the package?
The App is called banner_manager and in views.py I want to import a model called user from another project called django_models
when I try to import like this:
from ....models_django import models.py(in models.py it's the class "user" defined) it says: ValueError: Attempted relative import beyond top-level package

You can add this directory to PYTHONPATH for example:
export PYTHONPATH=$PYTHONPATH:/var/python/your-libs
And then just import package as normal:
import models_django

Related

Importing multiple models in create_app method

I have a base flask app that I have put together to build future projects from. I have it set up with an application factory method "create_app".
https://github.com/thenetimp/flask_base_v2/blob/master/app/init.py#L1-L32
In the create_app method, I am initializing the application object. Then pass it to the previously initialized db object eventually calling db.create_all to create the database from my model(s).
In order for this to work I have to import any model I may have into the create_app function. This isn't problematic for a small database with a few tables, but if I have a database with a large number of tables it seems like there should be a better way. from app.models import * doesn't work inside functions, so I have to ask is there another way to manage this?
in the code I was trying to do
def create_app():
from app.models import *
...
when all I needed to do was
def create_app():
import app.models
...
get so use to doing it one way I forgot about the other.

Could not import 'cookbook.schema.schema'

I am going through the official tutorial provided by graphene-python for their library.
I, like a few others I have seen online, am having some serious issues trying to simply import the schema file within the project folder (project_name/schema.py). For reference, the project_name is cookbook as it is denoted within the tutorial.
This is within my settings.py:
GRAPHENE = {
'SCHEMA': 'cookbook.schema.schema'
}
and this is in the schema file tiself (project_name/schema.py):
import graphene
import cookbook.schema
class Query(cookbook.schema.Query, graphene.ObjectType):
# This class will inherit from multiple Queries
# as we begin to add more apps to our project
pass
schema = graphene.Schema(query=Query)
The error that I am getting is:
Could not import 'cookbook.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: module 'cookbook' has no attribute 'schema'.
I have also tried a few other tutorials as well, but haven't had any luck. My project is on django 2.0.2 and graphene 2.0.1. Any help would be much appreciated.
Did you add this to your installed apps?
INSTALLED_APPS = [
'graphene_django',
]
the error says that cookbook has no attribute schema.
Therefore the import cookbook.schema is not working in your schema.py
the example says
import cookbook.ingredients.schema
I actually had incompatible versions of Django, Graphene and Django-environ.
To solve, I made a virtualenv using mkvirtualenv. After that, I was able to follow this tutorial without any issues. It is capable of being stood up without a virtual environment, but it was far easier to just define one and get moving with a clean slate.

"No module named" error when importing models from other apps

I'm trying to import a model from another app.
I have a folder named "apps" where all my applications live.
so in the models.py of app2, i have the line:
from apps.app1.models import Book
but for some reason, i get the "No module named app1.models" error.
I'm using django1.9 if that matters.
Any advice?
Thanks.
I think from ..app1.models import Book will help you. It's a var of relative path to import.

Django built-in User Model implementation

I'm trying to use the built in User model provided by Django in my app, but I'm not sure how to implement it (i.e., what does my models.py file actually look like?) I'm starting off by building a simple login form with the Django auth system.
I'm seeing multiple examples on the web where they import User but never define anything. Like so
from django.db import models
from django.contrib.auth.models import User
#nothing similar to defining User (i.e. 'class User: #fields')
What I'm assuming is by importing User, you don't have to define anything, since it's already been defined, but when I go to run "python manage.py sql 'name'" nothing is executed.
You don't need a models file for the user, because Django already supplies one. You just need to ensure that django.contrib.user is in INSTALLED_APPS.
The only reason you would need to import User into another models file is if you wanted to add a ForeignKey from one of your own models to the User model. In all likelihood, you do want to do that eventually; but you don't need to just in order to get the user models created.

Django: Can I have an app in a sub folder of another app?

I tried to put an app inside another app (Outer one is a facade into the inner one so it made sense to locate them that way), and it doesn't create a table for the model in that inner app. Is this normal? (the app is installed, and registered with the admin)
Django loads models by importing the models module of every package in the INSTALLED_APPS setting. For example, with an INSTALLED_APPS setting of ('django.contrib.admin', 'django.contrib.comments', 'spam.ham', and 'eggs'), Django will import models from django.contrib.admin.models, django.contrib.comments.models, spam.ham.models, and eggs.models.
If you are only listing your outer app in INSTALLED_APPS (we'll assume it's named eggs), then only the models from eggs.models are being imported and created. To get the models installed from your inner app, you will need to add it to the INSTALLED_APPS as well, like eggs.inner_app, so that eggs.inner_app.models will get imported. (To facilitate foreign keys, I'm pretty sure that if you import models from one app into another's models.py file, only the models defined in the models.py file being scanned get created.)