I have many apps and i want to activate the admin for all the models in my all apps.
I remember few months one of my friend did something that enables the admin site without any admin.py file
he did something in settings.py files with INSTALLED_APPS and all of the apps showed in the admin section
I am now not able to find that. Any one??
Your friend probably did something like...
from django.db.models import get_models
for model in get_models():
admin.site.register(model)
In one of his admin.py files.
I dunno, I'd only do this to test stuff. It's a bit too magical. Remember you'll have to explicitly unregister any models you may want to register again.
Just follow the instructions in the docs. They describe how to activate the django admin site here. You need to modify urls.py. That's it.
There are even comments in that file that tell you which three lines to uncomment.
I recommend you do include an admin.py in your apps because it is the Django convention and that some explicitness in what you trying to achieve is a good thing; the solution I use is the following default admin.py template for all new apps I start:
# Auto registers any new models with the admin, eventually you will want a tailored admin.py
from django.contrib import admin
current_app = models.get_app(__package__)
for model in models.get_models(current_app):
admin.site.register(model, admin.ModelAdmin)
Related
I am building an app that displays different courses. For each course there is a list of the different classes it has.
Some courses have two classes, some have eight, some have just one or in the future some courses may have 10 classes. It's up to the app's administrator when they register a new course
class Curso(models.Model):
clases = models.IntegerField(default=1)
content = ArrayField(models.CharField(max_length=30),blank=False)
This model will only be for the administrator.
I want to store the different classes (just their names) in an array. But there's no need to show 8+ fields if the admin is just going to fill one in... Or is that the correct approach?
I want the admin to have an integer field where she types in how many classes the course has and depending on that the array fields will be displayed.
I understand ArrayField has a size attribute where I can say how long the array is. Right? So my question is:
Is there a way to dynamically change the array size depending on what the admin types in in the "clases" field?
I need this to work in the admin app. I am new to Django and I'm finding the admin app a little bit hard to manipulate.
Well I did my research. And this is how it turned out:
models.py
from django.db import models
from django_better_admin_arrayfield.models.fields import ArrayField
# Create your models here.
class Course(models.Model):
...
clases = ArrayField(models.CharField(max_length=10),null=True,blank=True, size=8)
...
Turns out I just needed to use ArrayField with django-better-admin-arrayfield
It doesn't do exactly what I described in this question, but it works just as fine (even better) to visually edit the arrayfield in the admin page
Here's the repository django-better-admin-arrayfield
You just need to pip install it and then add it to your settings.py installed apps
INSTALLED_APPS = [
...
'django_better_admin_arrayfield',
...
In admin.py
from django.contrib import admin
from .models import Course
from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin
#admin.register(Course)
class CursoAdmin(admin.ModelAdmin, DynamicArrayMixin):
...
And that was it! makemigrations and migrate and... It didn't work. The problem was the add another button wasn't working. So I read the issues in the repo and someone wrote you should run collectstatic and it worked!!
Shout out to #nbeuchat, whoever that is, that who's answer in in this stackoverflow post was the only thing that helped me get out of this problem
I think it makes sense to organize my Django apps in a different way (by protocol):
📂myapp
apps.py
models.py
utilities.py
📂html
admin.py
urls.py
views.py
📂rest
serializers.py
urls.py
views.py
📂graphql
etc
This would move the admin.py file into the html folder, but sadly autodiscover does not seem to find it anywhere else than in the myapp folder. Is there a way to point to the correct location: perhaps in apps.py? I am sure a symlink would work, but not really what I am after.
There's nothing magic about either admin.py or autodiscover. All admin.py does is run any register calls on import, and all autodiscover does is look for an admin.py file in each installed app and import it. There's nothing to stop you importing your admin.py in its custom location from somewhere else, eg the models or views files.
This was somewhat tricky, but the comment by #Joran Beasley set me in the right direction.
For admin.py to be autodiscovered it needs to be either in the top level app folder, or be imported in a file. However... not every file works.
If you do from .html import admin
into __init__.py or apps.py: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
into models.py: Results in an importerror from models into admin. I think due to the circular nature of the imports.
However the following works: from . import admin inside the urls.py. Make sure to add a comment that this import is needed for autodiscover purposes.
I've been trying to unregister the admin for sites in django by doing the following:
from django.contrib.sites.models import Site
admin.site.unregister(Site)
However this gives me an error stating that "Site" is not registered (even though it showed up in admin before).
If I try doing the following I get no errors but "Site" stays in the admin:
from django.contrib.sites.models import Site
admin.site.register(Site)
admin.site.unregister(Site)
I need the sites app and cannot take it out of INSTALLED_APPS in settings. However the admin for it is utterly useless to me.
Any ideas on what I am doing wrong here?
Thanks!
The order of your INSTALLED_APPS setting is important.
When Django starts it will import the applications in INSTALLED_APPS in the order they are defined (https://docs.djangoproject.com/en/1.11/ref/applications/#how-applications-are-loaded). In your example above you were unregistering Site before Django had the chance to register is it.
There isn't much you can do in terms of troubleshooting except for reading the logs very carefully. After staring at them for a while you either become one with Django and understand it all, or you come here like the rest of us ;-)
My INSTALLED_APPS usually start with the django.contrib apps, then any 3rd-party applications, then my apps at the bottom. I only change this if I have a very good reason to.
I've long been curious about what
admin.autodiscover()
actually do. The document didn't say much about it:
Above we used admin.autodiscover() to automatically load the INSTALLED_APPS admin.py modules.
and
There is really no need to use autodiscover when using your own AdminSite instance since you will likely be importing all the per-app admin.py modules in your myproject.admin module.
If I don't uncommnet
# admin.autodiscover()
what functionality I will lose?
And for what consideration should I use or not use autodiscover?
As u said: autodiscover() load all admin.py from the apps folders. So you have in the /admin/ all the models that you use (from your own app or not).
I recommend to use autodiscover() if you are going to use the admin app.
P.D. additionally some app have their on autodiscover with more functionalities.
admin.py is executed whenever your django loads URLconf from urls.py, the autodiscover() will search for all the apps in INSTALLED_APPS one by one and executes the code in that file.
I want to monkey patch the user model from Django.
My code:
from django.db import models
from django.contrib.auth.models import User
User.add_to_class('secret_question', models.CharField(max_length="100"))
User.add_to_class('answer', models.CharField(max_length="100"))
User.add_to_class('DOB', models.DateField())
Where do I place this code so that python manage.py syncdb will create the correct table?
I tried the main directory models.py, I tried an app's directory's models.py (these two didn't produce the correct table), and I tried placing it in the settings.py of the project (error, couldn't run).
Please take a look at Storing additional information about users section of the authentication documentation. It suggests a cleaner way to add additional information to a User object.
If you'd like to store additional information related to your users, Django provides a method to specify a site-specific related model -- termed a "user profile" -- for this purpose.
If you really want to monkey patch user model, there already exists an app for this.
Django-primate
A modular django user.
This Django application monkey patches
django in order to have a custom User
model that plugs into the
django.contrib.auth application.