Django admin cannot delete/change instances - django

I am working on a Django project, and had registered all my models to admin. But due to some reason, I cannot change any of the instances of any model that I created.
This is how it shows:
This is the initial screen as I log in.
As soon as I click on add/change, instead of the usual screen it shows like this:
Here are my models.py and admin.py:
admin.py:
from django.contrib import admin
from .models import Plant, Unit, Equipment, EquipmentCategories, Job, Task_category, Subtask_category, SubtaskStatus, TaskStatus, Files, Filetype, Comment, Timelog, Report, Approvals, Task_flow, Subtask_flow
# Register your models here.
admin.site.register(Plant)
admin.site.register(Unit)
admin.site.register(EquipmentCategories)
admin.site.register(Equipment)
admin.site.register(Job)
admin.site.register(Task_category)
admin.site.register(TaskStatus)
admin.site.register(Subtask_category)
admin.site.register(SubtaskStatus)
admin.site.register(Files)
models.py:
from django.db import models
from django.contrib.auth.models import User
import django.dispatch
# Create your models here.
class Plant(models.Model):
name = models.CharField(max_length=200)
start_date = models.DateField(null=True,blank=True)
end_date = models.DateField(null=True,blank=True)
def __str__(self):
return(self.name)
class Unit(models.Model):
plant = models.ForeignKey(Plant,related_name='Plant', on_delete=models.CASCADE)
name = models.CharField(max_length=200)
unit_location = models.CharField(max_length=200, null=True)
start_date = models.DateField(null=True,blank=True)
end_date = models.DateField(null=True,blank=True)
def __str__(self):
return(self.name)
class EquipmentCategories(models.Model):
unit = models.ForeignKey(Unit,related_name='Unit', on_delete=models.CASCADE)
name = models.CharField(max_length=200)
def __str__(self):
return(self.name)
Can anyone please help!
Thanks in advance :)

I think something is wrong with your STATIC_URL in your settings.py file.
That fixed the issue for me.
It's not finding the right path to your static/admin css and js files.

This problem also occurs when you don't migrate your models properly in the terminal. Make sure to always migrate your models after having modified them.

Will be an issue with your user permissions.
Open the command line and cd /to/your/directory/with/manage.py and run python -m manage createsuperuser
Once you've created that login with those details and you should be able to update things.

Related

why i can't register my Django App models on the admin site?

but i see Groups and Users model by default if i try to register my model in app/admin.py using admin.site.register(model_name) if i try to registered the default Groups and User it will also not working for me.
CODE
Directory 'app/admin.py'
from django.contrib import admin
from app.models import Contact
admin.site.register(Contact)
Directory 'app/models.py'
from django.db import models
class Contact(models.Model):
name = models.CharField(max_length=100)
gender = models.CharField(max_length=50,choices=('female','Female')
('male','Male')))
email = models.EmailField(max_length=100)
info = models.CharField(max_length=100)
phone = models.IntegerField()
image = models.ImageField(blank=true,upload_to='images/')
date_added = models.DateField(auto_now_add=True)
def __str__(self):
return self.name
Change the import from "app.models" to just ".models" because they are in the same directory and see

Django MPTT Giving DatabaseError When Saving

I have installed MPTT for Django, put it in "installed apps," set up my files, and synced my database. My model shows in admin, but when I click save after trying to add a category I get the following error:
DatabaseError at /admin/myapp/category/add/
relation "django_admin_log" does not exist
LINE 1: INSERT INTO "django_admin_log" ("action_time", "user_id", "c...
Here are my files:
Models:
from django.db import models
from django.contrib.auth.models import User
from mptt.models import MPTTModel, TreeForeignKey
class Category(MPTTModel):
user = models.ForeignKey(User)
name = models.CharField(max_length=30, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
class MPTTMeta:
order_insertion_by = ['name']
def __unicode__(self):
return self.name
Admin:
from django.contrib import admin
from myapp.models import Category
from mptt.admin import MPTTModelAdmin
admin.site.register(Category, MPTTModelAdmin)
Is it possible, that you've enabled the admin logs feature without running syncdb afterwards? Here you'll find a very similar question.

What's the best way to keep queries ?

i'm poor with django.
i have a project and project has an app
in my app, i have a models.py and includes
from django.db import models
from taggit.managers import TaggableManager
class Post(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
created = models.DateTimeField()
tags = TaggableManager()
def __unicode__(self):
return self.title
and i also add this models.py
posts = Post.objects.all().order_by("-created")[:2]
Is it the right way to keep it here?
Some examples shows that queries in models.py some are in views.py ?
Also can i use posts in my mysite/templates ?
The best way to do this is to create a custom manager with a method that performs the query when called. That way you don't need to worry about it being cached, recycled, etc.

How to implement admin meta fields in Django 1.3 User profile?

from django.db import models
from django.contrib.auth.models import User
class MySiteProfile(models.Model):
# This is the only required field
user = models.ForeignKey(User, unique=True)
# The rest is completely up to you...
favorite_band = models.CharField(max_length=100, blank=True)
favorite_cheese = models.CharField(max_length=100, blank=True)
lucky_number = models.IntegerField()
The problem is that
User._meta.admin and MySiteProfile._meta.admin both return NoneType. I've dropped and recreated whole database, but no new fields appeared in admin panel; AUTH_PROFILE_MODULE is set.
There are a few ways you can make your MySiteProfile show up in the admin. One is to simply register your model with the admin, and it will show up under the app name it resides in.
Another is to unregister the contrib user from admin and instead load yours:
#admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_app.models import MySiteProfile
class MySiteProfileAdmin(UserAdmin):
def __init__(self,*args,**kwargs):
super(MySiteProfileAdmin).__init__(*args,**kwargs)
fields = list(UserAdmin.fieldsets[0][1]['fields'])
fields.append('favorite_band')
fields.append('favorite_cheese')
fields.append('lucky_number')
UserAdmin.fieldsets[0][1]['fields']=fields
admin.site.unregister(User)
admin.site.register(MySiteProfile, MySiteProfileAdmin)
There are quite a few articles around on this topic, but hope that helps you out.

'bool' object has no attribute 'has_header' when uploading via a FileField in my admin

My models.py
from django.db import models
from django.contrib.auth.models import User
class Song(models.Model):
uploader = models.ForeignKey(User)
date_uploaded = models.DateTimeField(auto_now=True)
song_file = models.FileField(upload_to='music/', max_length=100)
artist = models.CharField(max_length=75, blank=True)
title = models.CharField(max_length=100, blank=True)
genre = models.CharField(max_length=100, blank=True)
def __unicode__(self):
return u'%s' % (self.song_file)
My admin.py
from django.contrib import admin
from uploader.models import Song
from django.db import models
class SongAdmin(admin.ModelAdmin):
list_display = ('song_file', 'title', 'artist', 'genre', 'uploader')
search_fields = ('song_file', 'uploader', 'genre', 'title')
fields = ('song_file', 'title', 'artist', 'genre')
admin.site.register(Song, SongAdmin)
The file gets uploaded (I see it in my media folder) but it doesnt display in my admin page and when the file does get uploaded I get:
'bool' object has no attribute 'has_header' when uploading via a FileField in my admin
Am I missing something obvious here? Pretty new to django.
Turns out it was an issue with Pinax. Updated to the latest git and everything works now!
Found this somewhere else on the net, the problem is in the HideSensitiveFieldsMiddleware and the way to work around it (for debug) is to just comment out that middleware like so
"pinax.middleware.security.HideSensistiveFieldsMiddleware",
+# "pinax.middleware.security.HideSensistiveFieldsMiddleware",
in your settings file, probably should leave that middleware in for non debug environments