Key Value Storage for django ? editable in django admin - django

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

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

django apps and models.py in 1.7 - what exactly has changed?

Django 1.7 has introduced some apparently major changes for how apps work
https://docs.djangoproject.com/en/dev/releases/1.7/#app-loading-refactor
These release notes seem to be saying that you can define models outside of models.py and we don't even need a models.py (or models/__init__.py) inside an app.
Am I misunderstanding this? If not, could someone explain where we define our models if they are not in models.py and how does django find and load them?
You should still define your models in models.py.
Before the app refactor in 1.7 there wasn't a unified API for declaring metadata about your app. In particular, the way Django determined whether something was an app or not was by looking for a models.py file. That was not an elegant system, especially when you consider that some apps don't even have models (for example, the app might just provide management commands).
Now that the AppConfig API exists it's no longer necessary to require the existence of a models.py. However, it's still the natural, and default, place to define your models.
How does django find and load them?
From the documentation: "You must define or import all models in your application’s models.py or models/__init__.py."
That suggests the following practice: if you don't have any models, don't include a models.py. If you have a file's worth of models, put them in models.py. If you have a bunch of models and want to spread them out over multiple files, put the files in a models submodule and import their contents in models/__init__.py.

Set meta information for Django app

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

Sharing models between Django apps

I will be brief: to work in the spirit and idea of a Django app, it is ok for an app to import models from inside another app ? Say, a User statistics app will import models from a User app something like: from users.models import users
The answer is yes. It's perfectly okay for one application inside your django project to import models from another application. The power of a django project lies in the apps and their interactions.
Also make sure that you have utility applications importing models from more generic applications and not the other way. So "userstatistics" app should import models from the "users" app but "users" app should not rely on the "userstatistics".
If your app is importing models from a 3rd party application (lets say django-piston), be sure to specify that in a requirements file.
If you're building an internal app that has no chance of ever being released to the public, sure, do whatever you want.
If you're building an internal app that has little chance of ever being released to the public, but will possibly be used by future/current developers, sure, but be sure to document what the app needs to work properly.
If you're building an app for public release, try to keep it self-dependent (and django-internals dependent, ie, use what django provides, when possible). If you really need a third-party app to work, or if a third party app would make your code more manageable, then sure, include dependencies, but be doubly sure to document all requirements and necessary setup.
In most cases, you can do almost whatever you want so long as you have sufficient documentation.
I do, however, have to question the sanity of making your own User model that has the same name as django's builtin auth.User.
You cand try better extending the Django User model with inheritance. You will use the django user with custom field added, so you will have the same user for all the apps.
You can directly import models in app2/models.py. Usually you might need a foreign key, which looks like
models.ForeignKey('app1.ModelClass1', on_delete=models.CASCADE, related_name='modelclass2')
Don't do this. They will have the same app name and the ORM will be confused. Use an abstract model instead, and have both derive from it.

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.