Django admin not showing models - version 2.2 - django

I am using Django 2.2. I don't know what I'm missing.
models.py
from django.db import models
class Efesto(models.Model):
nombre = models.CharField(max_length=150)
tipo = models.ForeignKey(Color, blank=True, null=True, on_delete=models.CASCADE)
....
def __str__(self):
return self.nombre
admin.py
from django.contrib import admin
from estrategia import models
# Register your models here.
admin.register(models.Efesto)
Do I need anything else?
When I open the admin, I can't see the Efesto model there. The admin.py file is created automatically by the startapp command. The urls include
...
path('admin/', admin.site.urls),
It has being a while since I code django, and this used to be enough to get the models registered. The app is included in settings.INSTALLED_APPS correctly. Any advice will help.

You have to use admin.site.register(models.ModelName) in order to show the model in django admin.
You can find more about this in their official documentation
https://docs.djangoproject.com/en/2.2/ref/contrib/admin/

Related

Data in table is not showing in Django admin

Model:
from django.db import models
class VilleStation(models.Model):
nomVille = models.CharField(max_length=255)
adresse = models.CharField(max_length=255)
cp = models.CharField(max_length=5)
def __str__(self):
return self.nomVille
admin.py :
from django.contrib import admin
from prixcarbu.models import VilleStation
class VilleStationAdmin(admin.ModelAdmin):
list_display = ('nomVille', 'adresse','cp',)
fields = ('nomVille', 'adresse','cp',)
admin.site.register(VilleStation, VilleStationAdmin)
I imported a CSV file using database browser for SQLite. Table contains the data but admin page doesn't show it.
Do you see empty model in admin or no model at all?
If you don't see your model in the admin at all check if you added your app to INSTALLED_APPS in settings.py.
After closing my computer and reopening, the data is now visible in the admin. Probably a cache problem. Everything is fine now. Thank you all.

Django model database look up

I am generating video files triggered by POST request and saving them programmatically into the django model below.
How can I look up the uploaded file itself once uploaded to the database in a similar fashion to VideoUpload.objects(get)?
I don't have users only guests. I am using hash-id-field so far however can change this.
Models.py
from django.db import models
from django.conf import settings
import hashlib
from hashid_field import HashidAutoField
class VideoUpload(models.Model):
hashed_video_file_name = HashidAutoField(primary_key=True)
name = models.CharField(max_length=40)
videofile= models.FileField(upload_to='videos/', null=True)
objects = models.Manager()

Have registered class on admin, but it doesn't show up

This is my admin.py. I have created the file by myself and don't know if I need to register it somewhere in settings.
from django.contrib import admin
from .models import Athlete
admin.site.register(Athlete)
This is my models.py:
from django.db import models
class Athlete(models.Model):
firstName = models.CharField(max_length=30)
lastName = models.CharField(max_length=30)
Both files are in my project folder. I don't have any apps. When I go to url/admin/ I expect to be able to create and edit athletes, but I can only edit groups and users.
What more do I need to do to make Athletes editable in admin?
Add your module into settings INSTALLED_APPS list. Probably you forgot it (as you guess in your comment).

Django Admin tables not displaying correctly

Has anyone seen this before? I've tried making new apps, projects, etc.
All thats in my admin.py file is:
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
admin.site.register(Tribe)
admin.site.register(Membership)
admin.site.register(UserProfile)
I've not got any static files or css in the app..?
Create a class that inherit admin.ModelAdmin, update the fields to be shown in the list_display tuple, and register TribeAdmin instead of Tribe. Do the same for the rest.
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
class TribeAdmin(admin.ModelAdmin):
list_display = ('field_1', 'field_2',)
admin.site.register(Tribe, TribeAdmin)
# admin.site.register(Membership)
# admin.site.register(UserProfile)
For all the available options, have a look at the documentation or an easy to understand beginner tutorial from the DjangoBook (please note its for an outdated Django Version, but fields works with Django 1.8)
With Django 1.8 you can use.
#admin.register(Tribe)
class TribeAdmin(admin.ModelAdmin):
list_display = ('field',)

Import Error Django-profiles

I'm trying to get the django-profiles work.
I follow the steps of this manual ("The Missing Manual"),
so:
I work in the same project I also use for the django-registration (so no app created!!!)
In my templates I've created a folder "/profiles" (including edit_profile.html)
My AUTH_PROFILE_MODULE is set to 'myProjectName.UserProfile'
I've created in my project folder models.py containing:
from django.db import models
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(_('first name'), max_length=100)
middle_name = models.CharField(_('middle name'), blank=True, max_length=100)
last_name = models.CharField(_('last name'), max_length=100)
birth_date = models.DateField(_('birth date'), blank=True, null=True)
In my project folder, I've created forms.py, with exactly the same code as in the manual
In urls.py of my project I've added:
from myProjectName.forms import ProfileForm
('^profiles/edit', 'profiles.views.edit_profile',{'form_class':ProfileForm,'success_url':'/my/custom/url',}),
(r'^profiles/', include('profiles.urls')),
So now the whole urls.py file of my project is:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from django.contrib import admin
from myProjectName.forms import ProfileForm
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', admin.site.urls),
(r'^accounts/', include('registration.urls')),
(r'^$', direct_to_template,
{ 'template': 'index.html' }, 'index'),
('^profiles/edit', 'profiles.views.edit_profile', {'form_class': ProfileForm,'success_url':'/my/custom/url',}),
(r'^profiles/', include('profiles.urls')),
)
When I access now http://127.0.0.1:8000/profiles/edit/, I get this error:
Exception Value: No module named ourcrestmont.itaco.models
That's the import located in forms.py ...
What am I doing wrong?
EDIT:
The code of my forms.py:
from django.db import models
from django.forms import ModelForm
from ourcrestmont.itaco.models import *
class ProfileForm(ModelForm):
class Meta:
model = Foo
exclude = ('field1','field2','field3',)
It's exactly the same code as in the manual, or is this code not good? (could anyone give an other, better code?)
So that import statement appears to be attempting to import a file called "ourcrestmont/itaco/models.py". Make sure that exists.
(by django model, that looks like it is a project called ourcrestmont with an app called itaco)
If it exists, make sure that itaco and ourcrestmont both have init.py files in them.
You can just have the project (the import line in that case is from projectname.models import modelname) but I'd recommend sticking to the standard layout and putting your models, forms and views together in an App beneath a project, even if you only currently see your project as having one App. (Because it doesn't, it has at least three, since registration and profiles are also django apps, they're just django apps outside of what you've made, if you see what I mean, and thinking of it like that might make dependancy and inclusion bugs easier to solve. Certainly every other django app is going to assume most things are inside an app)