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.
Related
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.
I need to add admin.py in my app folder.
Currently the app folder contains the following files:
__init__.py
models.py
test.py
views.py
Any help will do :-)
The tutorial say to edit admin.py in the polls directory. However, if you're just starting a new project admin.py will not exist.
You can simply create a blank admin.py file and add the contents suggested by the tutorial. Django will automatically notice any admin settings from this file when the admin app is enabled.
For some reason, when I add a new model and use Django South to sync the database by: /manage.py schemamigration myapp --auto and then the migrate line, I still can't see the model on the admin page.
South does say that it added the model though.. so I'm not sure what's going on..
Any thoughts?
Just because you created the model and synced it, does not mean it gets added to the admin page automatically. You must create an admin.py file in your app directory that contains
from django.contrib import admin
from .models import MyModel
admin.site.register(MyModel)
and make sure you have admin.site.autodiscover() in your main urls.py
This should all be covered in the tutorial pages for Django. Go back and RTM.
I have been given a small legacy db (mysql) to work with, and therefore, I have tried to use the inspectdb command in django.
So, I use something like:
python manage.py inspectdb > models.py
It works fine, and I take the models.py and copy the class it created into my app (searchapp) as a models.py file.
I have done a syncdb, and everything went fine. Results are as expected.
Now, I have tried to add this model into my admin site, using:
from searchapp.models import Abaqus
from django.contrib import admin
admin.site.register(Abaqus)
stored in a file called admin.py (Abaqus is the name of the class generated by inspectdb and searchapp is the app name). However, I am not able to see the app on the admin site. I have checked my settings.py and it includes the 'searchapp'.
I was wondering if someone could point me in the right direction..
I suspect admin.py is not loaded. You could check this by putting a debug statement just above the register call:
import pdb;pdb.set_trace()
admin.site.register(Abaqus)
If this is in fact the case, the correct way to ensure admin.py is loaded is to call django.contrib.admin.autodiscover() at the beginning of your main url conf.
If you've written no admin classes and don't want an admin.py, you can call admin.site.register(Abaqus) right below where the model is defined, inside models.py.
If you have an admin module structured like the following, import admin within models.py to ensure the code is run:
myapp
|--models.py
|--views.py
|--admin
|--__init__.py
|--views.py
|--base.py
Another possible cause would be that you are missing the permissions to edit the model. Double check that you are logged in as a superuser.
In DjangoBook, it says that the every books app should have its own admin.py. However, none of my apps have there own separate admin.py as the text suggests. I was just wondering if this is a Django 1.3 thing and if so, where is the admin.py data stored now, if not in a separate admin.py file?
The chapter I'm referring to is here:
http://djangobook.com/en/2.0/chapter06/
P.S. I'm not talking about django-admin.py
By default there is no admin.py file created for you when you create a new app, you will need to create your own. Here are the directions on how to create the admin.py file.
http://docs.djangoproject.com/en/1.3/ref/contrib/admin/
Edit: 1.3 is no longer supported, here is a link to 1.8:
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/
You need to create admin.py in new app and edit it with from django.contrib import adminIn project/urls.py, in my case (mysite/urls.py) specify the url of newapp/admin (polls/admin) likefrom django.conf.urls import patterns, include, url from django.contrib import admin
admin.autodiscover() urlpatterns = patterns('',url(r'^admin/', include(admin.site.urls)),)