Set meta information for Django app - django

I have a few apps installed on my Django project. I would like for some of the apps to be able to get information from some of the other apps. Therefore, I am looking to give each of the apps some basic meta information about permissions.
So, if I have three apps: A1, A2, and A3.
I would like to set A1 to be read only. Set A2 to be read/write. Set A3 to not be able to read or write from.
The meta descriptions are mostly for my own sanity. These apps won't be working with apps that I didn't write.
Is there a way to add such meta data at the app level? Perhaps through an unused description field for the Django app?

Don't know why you would ever need this, but normally you place all meta information in __init__.py of the app. Then you can access it from another app:
myapp1/__init__.py:
version = '0.1.0'
permissions = ['read', 'write']
myapp2/utils.py:
from django.conf import settings
import importlib
for app_name in settings.INSTALLED_APPS:
app = importlib.import_module(app_name)
if hasattr(app, 'permissions') and 'write' in app.permissions:
do_something()

Related

Using Django framework only with admin backend without any apps

I want to use Django only with admin backend without any apps. So actually all I want to do is to use the admin backend to CRUD my database. Now apparently the admin backend does not have a models.py and no views.py.
Do I really need the models.py from an app, or can I easily use only the admin backend to CRUD my database. How would I do this, add a models.py to the admin backend?
First of all, if you want to CRUD something, you will need a model so you can interact with your database (SQLite, Postgres, etc).
However, a model belongs to an app, once this is the core of Django. So, take a look at https://docs.djangoproject.com/en/2.2/topics/db/models/ where you can read more about that.
If you need a tutorial, take a look at https://docs.djangoproject.com/en/2.1/intro/tutorial02/
In summary, yes, you need an app. However, you do not need a view, once there will be no router, I suppose. Just expose your model to the admin, for instance:
from django.contrib import admin
from .models import YourModel
admin.site.register(YourModel)
Hope it helps

How to store third party apps migrations in django

I'm fairly new to python and django, and trying to build a simple calendar based on django-scheduler package.
According to django-scheduler docs, a custom base class can be used to add additional fields, managers and such.
So, I used an abstract model to add a new field:
#myproject/customer_calendar/models.py
from django.db import models
from main.models import Customer
class CalendarAbstract(models.Model):
customer = models.OneToOneField(to=Customer, null=True, blank=True, related_name='calendar')
class Meta:
abstract = True
And added this to settings.py
SCHEDULER_BASE_CLASSES = {
'Calendar': ['customer_calendar.models.CalendarAbstract'],
}
Now, if I use makemigrations command, a new migration is created inside scheduler app (which is located in site-packages of the current virtual env), which doesn't allow me to keep track of migrations via VCS.
I've found a couple of solutions:
1) Keep the whole scheduler app inside my project. According to SO it' s considered a bad practice and third-party apps should always be retrieved via pip.
2) Use django setting to store all django-scheduler migrations inside my calendar app
MIGRATION_MODULES = {
'schedule': 'customer_calendar.migrations',
}
The second one looks good to me, but I don't know if it's considered to be a valid solution to this problem.
Is there any other ways to store third-party apps migrations?
The second one looks good to me, but I don't know if it's considered
to be a valid solution to this problem. Is there any other ways to
store third-party apps migrations?
As also stated in this answer, FeinCMS docs recommend the use of MIGRATION_MODULES to monitor the migrations of FeinCMS as a third-party app.
FeinCMS itself does not come with any migrations. It is recommended
that you add migrations for FeinCMS models yourself inside your
project.
...
Create a new folder named migrate in your app with an empty init.py inside.
Add the following configuration to your settings.py:
MIGRATION_MODULES = {
'page': 'yourapp.migrate.page',
'medialibrary': 'yourapp.migrate.medialibrary', }
You must not use migrations as folder name for the FeinCMS migrations,
otherwise Django will get confused.

Django Directory Structure

I am new to learning Django and I know how important setting up an application directory structure can be for future additions.
At the moment, I am creating an API using the Django REST Framework and have a few questions on how to structure a Django project for future success.
I want the API to feed out possibly in the future to other outside sources that may need to grab data. I also will be building a front-end CRUD like system to display and update data.
How would you suggest structuring the directories in the future possibility of adding more front-end like systems that are all powered by the Data API? Doing some research already, it seems like these are possibilities I have seen.
project
manage.py
project
settings.py
urls.py
api
models.py
serializers.py
views.py
crudapp
files here...
project
manage.py
project
settings.py
urls.py
api
models.py
serializers.py
views.py
crudapp
files here...
project
manage.py
project
settings.py
urls.py
crudapp
api
models.py
serializers.py
views.py
files here...
futureapp
api
models.py
serializers.py
views.py
files here...
I am really trying to understand how Django and Python should have these modules setup. If I have separate modules/apps setup, they can all access the models in the API app for any sort of app I build in the future?
Any clarification or experience on this would be greatly appreciated. The main goal is to have an API for the data in the database, and to then be able to build a front-end CRUD to update and pull that data, with the possibility that more front-end type apps could be built in the future as the data set grows.
For example, I am building a corporate data CRUD system to store important corporate information that will be accessible by a front-end and also an API that can feed to other non DJANGO websites. In the future, I may be adding an entirely different front-end/back-end CMS type system for a single page application. However, some of the data in the database from the corporate data set may be used to populate the website data.
i would take structure like this:
project
manage.py
apps
crudapp
futureapp1
futureapp2
so you can add as many apps as you wish and main project folder doesnot grow
and one more advantage is, your imports will be more beautiful and less name clashing in APPS:
from project.apps.crudapp.models import OneModel
# settings
INSTALLED_APPS = (
'project.apps.crudapp',
)

Key Value Storage for django ? editable in django admin

I need some good way to provide admin users of my django 1.5 app, manage some various app parameters, these parameters or settings are different from the app core settings, ... can anyone point some directions for this ??
thanks everyone
You can create an app and use a Model to handle all the possible parameters that you need to use. Then register your admin for this app, and you will be able to change an use this values.
I always have a utils app, that I can put some code that can be shared within the project and along all the projects that I work, so I can give you a suggestion to put this in your utils app:
#models.py
from django.db import models
class Parameters(models.Model):
default_product_price = models.IntegerField(default=10) # you can change this in admin
...
Then, register your classes in admin.py

How to use admin interface if I have no application?

I am creating a Django based app and I'd like to put everything under the root in the following structure:
/path/to/my/app/
settings.py
models.py
urls.py
admin.py
...
One problem that I run into is the admin interface doesn't include whatever models I have that are registerd in admin.py usin
admin.site.register(models.MyModel)
Usually that's done by using auto discover in urls.py, but now I have no registered "app", the auto discover doesn't work anymore. Is there anyway I can still use the admin interface?
Thanks.
Django simply doesn't work without apps. They're the fundamental building block of a Django site. A whole range of things, not just the admin, will fail to work. Why do you want to do this?
Putting the app in the django-style directory structure will make your project easily extensible if you decide to add functionality later.