Django recommended structure TastyPie - django

Using TastyPie (API framework for Django) what is the recommended file structure (or in your opinion).
Have a different resources in each app something like this:
app1
models.py
resources.py
app2
models.py
resources.py
Or create a new app say and have one resource one resource:
app1
app2
apiApp
resources.py

Here is very simple and efficient structure of tastypie api.
Projectname
app1
api
__init__.py
resource.py
models
__init__.py
model1.py
model2.py
migrations
__init__.py
__init__.py
app2
api
models
migrations
__init__.py
common
__init__.py
constant.py
authentication.py
commondata.py
middleware
__init__.pt
cors.py
projectname
__init__.py
settings.py
urls.py
wsgi.py
manage.py
Projectname.wsgi

Creating a new app for just resources does not make sense. I would go with having different resources in each app. Alternatively you can have them in /resource.

Related

Django AppConfig ready()

I have Django 2.1.7
I read Django documentation as well as, this How to use django AppConfig.ready() and this Overriding AppConfig.ready().
Now, the folder/file structure is like this,
my_app
__init__.py
urls.py
sub_app1
migrations
__init__.py
admin.py
apps.py
models.py
...
sub_app2
...
sub_app3
...
and all sub_apps are registered into INSTALLED_APPS of Django settings.py file as my_app.sub_app1 for example. I noticed that when I try to override ready()for sub_app1 w/in sub_app1.apps.py, overriding has no effect.
When I flatten my structure above as
my_app
__init__.py
urls.py
apps.py
overriding of ready works fine.
In other words, it seems that overriding of readyof a sub app w/in sub app's apps.py doesn't work.
Also, whether using default_app_config w/in sub app's __init__.py or my_app.sub_app1.apps.SubApp1Config w/in settings.py INSTALLED_APPS it gives error django.core.exceptions.ImproperlyConfigured: Cannot import 'sub_app1'. Check that 'my_app.sub_app1.apps.SubApp1Config.name' is correct.
Why is this happening?

Django apps sharing a model

I Django can 2 apps share 1 model, or 2 apps must define the same model inside. so app1 and app1 can have have same products model inside them for example?
Yes, app1 and app2 can share the same model. You need to import it wherever you want to use it.
Lets say your project structure is like below having 2 apps app1 and app2.
my_project/
manage.py
my_project/
__init__.py
settings.py
urls.py
wsgi.py
app1/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
app2/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
Then to use the models defined in app1/models.py in app2, you just need to do:
from app1.models import MyModel # import the model
Just import the model from the app that defines it to the app that is using it.

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'
)

How do I customize the admin page in Django 1.7?

I've been learning from the Django Documentation and have stumbled upon a road block in the section: https://docs.djangoproject.com/en/1.7/intro/tutorial02/#customize-the-admin-look-and-feel
My files are arranged as:
mysite/
mysite/
__pycache__/
...
__init__.py
settings.py
urls.py
wsgi.py
polls/
__pycache__/
...
migrations/
...
__init__.py
admin.py
models.py
tests.py
views.py
templates/
admin/
base_site.html
db.sqlite3
manage.py
I have modified the text in the file base_site.html to "Polls App" but the admin site continues to display "Django Administration".
PS: I'm using Win8
You need to place that templates folder in the project directory, not on the same level as the project as it currently is.
So place it under mysite/templates and you should be close.
EDIT: Actually, put the templates folder under your application so that it is Polls/templates
I have it at the same place as you, right under the project root. Make sure that django.contrib.admin is defined before your polls app in INSTALLED_APPS.

Where should i create django apps in django 1.4?

I've just started a new project in django 1.4 and since they've changed the default layout for manage.py and the whole folder hierarchy (see https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py) i cannot decide where should i put my app packages - inside mysite or outside it? What's the best practice? For some reason, startapp command creates the app outside of the mysite package, but this feels somehow wrong.
So, what's the best? This:
manage.py
mysite/
__init__.py
settings.py
urls.py
myapp/
__init__.py
models.py
or this:
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py
?
The second way.
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py