Import relative apps in Django - django

I'm new to Django and have a question about app structure and imports. My project structure looks like this (from deploydjango.com):
root/
manage.py
mysite/
__init__.py
urls.py
wsgi.py
settings/
apps/
__init__.py
profile
__init__.py
models.py
views.py
video
__init__.py
models.py
views.py
photos
__init__.py
models.py
views.py
Basically I will have some profiles on my site, where each profile then have a number of uploaded photos and videos. So in my video model I have the following code:
from django.db import models
from XXXXX import Profile
class Video(models.Model):
# more fields
company = models.ForeignKey(Profile, related_name='videos')
How do I import the Profile model for use in the Video model? Or is my structure not suitable..? What would be best practice for this?

I think your structure is fine. Basically, python's modules are just a python file which you may import like this: import <filename> and you will have its functionality imported. But when you group some python files in a folder and include in that folder a file called init.py there you have a python package. With is a better way to manage a group of modules and it allows the dot notation in your imports. Django apps behave like packages as you may see.
If you want to import Profile within your video module then you have to do this:
from profile.models import Profile
That is, assuming that the Profile model is defined in your profile app in models.py. That should do.
If you want more information about python project structure this is a great place.

Related

Django Model Import Error: ValueError: attempted relative import beyond top-level package

I have created a new app "grn" in my django project and tried to import the models from another app named "packsapp" in the same project like this:
Models.py
from ..packsapp.models import *
But I got the following error:
ValueError: attempted relative import beyond top-level package
Here's the structure of the app:
yantra_packs
grn
--migrations
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
media
packsapp
--migrations
templates
templatetags
views1
__init__.py
apps.py
decorators.py
forms.py
models.py
urls.py
views.py
How can I import the models of the packsapp in the grn ??
The root directory of a Django project is not a Python package or module. So relative imports across Django apps will not work. Use an absolute import instead:
from packsapp.models import *

Flask Blueprints sharing

I want to make an API with Flask and it also needs to have an admin panel.
I guess that Blueprints are the way to go, but I don't want to make models twice.
My structure is going to be this:
- app
- api
- admin
- models
So my question is: How can I access the models in the models folder in my api blueprint and my admin blueprint?
Thanks in advance.
if you'd in a module within the api or admin folders you can import anything from a module in the models folder using this notation
from ..models.module_name import model1, model2, etc
for small projects i usually keep all the models in a single models.py file like:
[app]
[blueprint_1]
__init__.py
views.py
[blueprint_2]
[static]
[templates]
__init__.py
models.py
then from within any of your blueprint files just:
from ..models import model1, model2, etc
About the import, If your directory include __init__.py then it is a python package so . use for current dir. For example:
auth/
__init__.py
forms.py
views.py
#views.py
from forms import Form name
from . import auth_blueprint # imports from __init__.py
So if you wants to import from another directory you have to use .. to imports from __init__.py file let's say your models directory include those files :
models/
__init__.py
UserModel.py
Now let's import models for auth module :
#auth/views.py
from .. import models # import froms models/__init__.py
from ..models import UserModel

Cannot import python social auth into Django app that uses custom login

Using Django 1.6.5, created a custom login framework for username/password token-based logins but now want to implement some social login too. The only problem with this, is that every example in existence is using Django's built-in templates and so forth which are not helpful.
In trying to use python-social-auth I've discovered that I don't understand anything about relative imports and despite reading many SOs about it, I'm still confused as to how I should go about referencing my Django app in /home/django/project from the pip-installation location of python-social-auth: /usr/local/lib/python2.7/dist-packages/social.
Now why would I want to alter any of this code you may be wondering...well it's quite simple really in that I've already created this username/password login system based off of tokens so I'm trying to figure out a way to give social-authed users a token as well. The only way I see that to be possible is by accessing my custom UserProfile object from my existing models, while inside the python-social-auth code and then slapping a token on users when they login. The file I'd need to alter is actions.py which you can see the location of in the diagram below.
I've already tried by using pip uninstall python-social-auth and actually just copying the entire folder of social auth code into my project in /home/django/project, the references work just fine there and no need to refactor anything...except again I can't import my own models from my Django app since it's 'outside' of the social module. So I guess I have two or three questions.
Should I leave (and edit) the python-social-auth code in place at
/usr/local/lib/python2.7/dist-packages/social (scenario #1) or
should I just pip uninstall it, and copy the social folder into my
own project for editing it there(scenario #2)?
Depending on which scenario I choose, how do I properly import models from my project into the social code? I will outline the directory structure of each
scenario...generally in my project when I need to import the UserProfile object I do: from apps.app1.models import UserProfile but that clearly won't work if I try to import it into a file living at /usr/local/lib...
Scenario #1 (custom UserProfile object is inside app1 models.py):
/usr/local/lib/python2.7/dist-packages/social/
__init__.py
actions.py
apps/
__init__.py
django_app/
__init__.py
default/
__init__.py
project/
manage.py
__init__.py
apps/
__init__.py
app1/
__init__.py
app2/
__init__.py
app3/
__init__.py
Scenario #2 (I copy the entire /social/ folder into /home/django/project):
project/
manage.py
__init__.py
apps/
__init__.py
app1/
__init__.py
app2/
__init__.py
app3/
__init__.py
social/
__init__.py
actions.py
apps/
__init__.py
django_app/
__init__.py
default/
__init__.py
Clearly writing out how to import UserProfile from app1 inside the actions.py file in either scenario is what I'm looking for. Or better way to do what I'm trying to do...
You should use the pipeline feature in python-social-auth to setup that token, for example (pseudo code):
def set_token(user, is_new=False, *args, **kwargs):
profile = get_or_create_profile(user)
profile.token = "new token"
profile.save()
Let's say you put that function in a file at project/apps/app1/pipeline.py, then add this entry to your settings:
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details'
'apps.app1.pipeline.set_token'
)

Django 1.5.1 - Admin.py missing while running startapp

I've been following the DjangoProject tutorial. When I run python manage.py startapp newapp while in the same directory as manage.py. In the newapp directory I see init.py, models.py, tests.py, and views.py but not admin.py file. Where is admin.py?
I am running Django 1.5.1 in Windows 8
You have to create an admin.py file.
you don't necessarily need an admin.py file,
just import the admin module in your models.py file,
from django.contrib import admin
and for each model do the following:
admin.site.register(model1)
admin.site.register(model2)
However, this is not best practice, but since it's just a tutorial, it will work.
You also need to uncoment the relevant lines in the urls.py file
I think I had the same frustrations following the DjangoProject tutorial - however, when I cross-referenced it with with the DjangoBook tutorial (for the same version, I believe, 1.5.1), I found that an admin.py file was not necessarily created after a python manage.py startapp xyz command -- moreover, I also uncommented all of the admin options in urls.py, views.py, and settings.py - so a bit of a mix of what Neal and Ibrahim said
You have to create your own admin.py file in the app if you want it. Indeed, this file is optionnal and isn't created by startapp.
If you want a default template to begin your admin.py, it should be:
from django.contrib import admin
from models import Model1, Model2
class Model2Admin(admin.ModelAdmin):
list_display = ('title', 'content', 'date')
# Just an example, chekc docs and tutorials for more info.
admin.site.register(Model1)
admin.site.register(Model2, Model2Admin)
The reason there is no default admin.py is because you don't have any models yet when you create your new application; so there is nothing to add to the admin section.
Further, you may not want to admin all the models in your application; or you may be creating an application that does not need any admin hookups; or you may not be using the admin application at all in your project.
Since django cannot decide this for you, there is no default admin.py generated.
To create one, if you are following the tutorial - simply keep reading and in part two you'll create the admin.py file when you learn about the admin contrib app and how to integrate it with your custom models.

ImportError After Moving App to Nested Folder

My application was working fine when I wanted to see whether I could organize my project in a better way. I read through this tutorial on structuring a django project.
Before my project structure was as follows:
camucamu
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
What I wanted to do was move the books app into an apps folder. Thus I did that and changed the project structure to the following:
camucamu
apps
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
I then changed the imports in views.py and admin.py
from books.models to apps.books.models.
I also changed INSTALLED_APPS in settings.py from books to apps.books.
When I then tried to run syncdb, I get the following error:
raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError apps.books: No module named apps.books
What am I messing up here so it can't find my app anymore?
Your apps folder does not have an __init__.py file so it cannot be recognized as a python module
I got the same error, following the same guide, as the last point of the following list was not cited. Make sure you performed the following changes:
Create a blank __init__.py file inside the apps folder (needed for python to recognize it as a package)
Update the import statements wherever you refer to an external app:
from projectname.apps.appname.models import YourModel, YourOtherModel
Inside settings.py edit INSTALLED_APPS such that it looks like this:
INSTALLED_APPS = (
...
# apps
'projectname.apps.appname1',
'projectname.apps.appname2',
)
This one is not specified in the guide: In all your urls.py files, update the urlpatterns!
BEFORE:
# client views
urlpatterns += patterns('appname',
...
)
AFTER:
# client views
urlpatterns += patterns('projectname.apps.appname',
...
)
Finally remember to update your changes by calling python manage.py syncdb
Hope that helped.