Model field not displaying in Django Admin - django

I have a Django project hosted on heroku
I added a new slug field to model
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.name
migrated it using south on heroku. Checked the heroku postgresDB as well for added field. All fine.
Opened Admin. No slug field showing...
added slug to fields[] in admin.py. Still not showing. Here is admin.py
from django.contrib import admin
from models import Category
class CategoryAdmin(admin.ModelAdmin):
fields = ('name', 'slug')
admin.site.register(Category, CategoryAdmin).
I even did a heroku restart... No change.
What can be done to show it ?

Try to use list_display like following:
from django.contrib import admin
from models import Category
class CategoryAdmin(admin.ModelAdmin):
fields = ('name', 'slug')
#list of fields to display in django admin
list_display = ['id', 'name', 'slug']
#if you want django admin to show the search bar, just add this line
search_fields = ['name', 'slug']
#to define model data list ordering
ordering = ('id','name')
admin.site.register(Category, CategoryAdmin).

Just in case someone ever faces this scenario
My admin classes were inheriting from UserAdmin, when they should have been inheriting from admin.ModelAdmin.
I had to change
class Model1(UserAdmin):
....
to
class Model1(admin.ModelAdmin):
....

I see the solution in here Django Website: https://docs.djangoproject.com/en/3.2/ref/models/fields/#editable, use fields's editable property.
editable
Field.editable
If False, the field will not be displayed in
the admin or any other ModelForm. They are also skipped during model
validation. Default is True.
I have posted a png image before, but I don't know how to display it.

Related

djongo ArrayField not appearing in Django Admin

I am following the official docs of Djongo mapper => https://www.djongomapper.com/using-django-with-mongodb-array-field/ to add Array Fields to my Model, but unfortunately even after adding the Array Fields as stated in the docs I am unable to see them in the below view shown in the docs.
Here is the Model defined by me.
from djongo import models
from django import forms
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
class Meta:
abstract = True
def __str__(self):
return self.name
class EventModel(models.Model):
_id = models.ObjectIdField()
authors = models.ArrayField(
model_container=Author,
)
def __str__(self):
return self._id
Here is the generated Django Admin view based on my Models, which is no way similar to the one shown in docs here.
https://www.djongomapper.com/using-django-with-mongodb-array-field/
Any help on this would be appreciated thanks :)

Autofield column is not showing in admin database

i have created a model Post
class Post(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='blog_image', default='default.jpg')
smallContent = models.TextField()
content = models.TextField()
data_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
but id field is not showing in admin panel. i did makemigrations and migrate and both are done successfully.
AutoField is generated after the model is saved. It is not provided in Admin Panel as it should not be editable.
If want to make id editable (not recommended) override it as a CharField instead of AutoField with primary_key=True.
Else if just want to show it in the panel (while editing a saved model), add it to the read_only list of your model admin.
Read about AutoField here.
Since it is a readonly field I think you probably need to explicitly tell admin to show it.
I don't know if this will work but try in admin.py
from django.contrib import admin
from .models import Post
class PostAdmin(admin.ModelAdmin):
readonly_fields = ['display_id']
def display_id(self, obj):
return obj.id
admin.site.register(Post, PostAdmin)

How to show Django timestamp in backend

I'm using Django 1.6 as backend, what I want to do is to let users write their comments while watching the video. I wrote the field
datetime = models.DateTimeField(auto_now_add=True)
in the model.py, but in the Django back-end, I didn't see this column, how can I let it show in my admin? Thanks!
Just add readonly_fields option to you admin.py:
class CommentAdmin(admin.ModelAdmin):
readonly_fields = ('datetime',)
If you have fields option defined in you admin class, you'll also have to add datetime to the fields option:
class CommentAdmin(admin.ModelAdmin):
# display datetime in the changelist
list_display = ('id', 'title', 'datetime')
# display datetime when you edit comments
readonly_fields = ('datetime',)
# optional, use only if you need custom ordering of the fields
fields = ('title', 'body', 'datetime')
admin.site.register(Comment, CommentAdmin)
For more info, please see:
https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields
You can set a field of a model to be shown in admin by adding it to the ModelAdmin (in admin.py of the model's app):
from myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
list_display = ('datetime',)
admin.site.register(MyModel, MyModelAdmin)
And to set the short description that is displayed next to the field, you need to set verbose_name of the field, like the following (in models.py):
class MyModel(models.Model):
datetime = models.DateTimeField(auto_now_add=True, verbose_name="Uploaded at")
Note: You don't need to set readonly_fields since DateTimeField with auto_now_add=True arg will be read-only by default.

Many-To-Many Fields View on Django Admin

Basically I am trying to recreate a fields in this model that is similar to django user authenticate system, for this I try using many-to-many fields but it ended up like this.
I am trying to have another field that can show what exist and another field for I have chosen similar to django admin, which looks like this.
This is my code
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
class BlogPage(models.Model):
category = models.ManyToManyField(Category)
title = models.CharField(max_length=128)
preview = models.TextField(max_length=256)
content = models.TextField()
I believe what you want is a filter_horizontal widget used in the Admin template. This should help: Django Admin ManyToManyField
currently i am able to display many to many fields is admin panel
models.py
class Abc(models.Model):
course = models.ManyToManyField(Course, blank=True)
admin.py
class AbcsAdmin(admin.ModelAdmin):
"""
Admin panel management for Alumni
"""
list_display = ["id","get_course"]
def get_course(self,obj):
return [course.name for course in obj.course.all()]

Django admin: use one-to-one relationship in search_fields

I am trying to to add a search to my model admin list page using the following Model and ModelAdmin classes:
models.py
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
country = CountryField(blank=True, null=True)
admin.py
from django.contrib import admin
from models import UserProfile
class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user','country')
search_fields = ['user']
But I get the following error while trying to access the UserProfile in admin panel:
at /admin/profiles/userprofile/ Related Field has invalid
lookup: icontains
I have also tried the following:
search_fields = ['user_username']
And
search_fields = ['user_name']
def user_name(self,obj):
return obj.user.username
Any solutions?
Try using user__username, according to the lookup API “follow” notation.