I am new with python and django, i want to know how can i implement the admin search bar on my project model? i notice that the user model has it by default.
My code is below, after the makemigration command still no search bar i think i am missing something. sorry for the noob question.
class Todo(models.Model):
search_fields = ('title', 'text', 'created_at',)
title = models.CharField(max_length=200)
text = models.TextField()
created_at = models.DateTimeField(default=datetime.now)
def __str__(self):
return self.title
in the apps admin.py
inside the class for which you are registering the model
add
search_fields = ['column_name']
add search fields in admin file instead of models file
Related
I am working with wagtail cms and want to use ArrayField. The default form for it is very inconvenient. I found a python package that has a better form field that is developed for Django admin. Also in the wagtail repository, I found an issue where an author states that he was able to make django-better-admin-arrayfield work with wagtail. I made several attempts but failed. Can anyone help me do it?
Update:
Model:
class Reaction(models.Model):
category = models.ForeignKey(ReactionCategory, on_delete=models.CASCADE)
name = models.CharField(max_length=128)
randomized_sentences = ArrayField(models.TextField(blank=True, null=True), null=True)
panels = [
FieldPanel('category'),
FieldPanel('name'),
FieldPanel('icon'),
FieldPanel('randomized_sentences', widget=DynamicArrayTextareaWidget)
]
ModelAdmin:
class ReactionAdmin(ModelAdmin, DynamicArrayMixin):
model = Reaction
menu_label = 'Reactions'
menu_icon = 'comment'
list_display = ('name', 'category')
search_fields = ('name', 'category__name')
I also added django_better_admin_arrayfield.css and django_better_admin_arrayfield.js to my static files directory. With all this done, The edit control for randomized_sentences looks like this: It not only looks not as expected but also adds additional '`'s in unexpected places.
I'm learning Django right now and I made this class called Clowns. On the Django Admin page I made two test objects.
I made four attributes(dunno what they're called lol) for clowns. They are title, description, identification, and hobbies. See below:
title = models.CharField(max_length=20)
description = models.TextField()
identification = models.CharField(default='-', max_length=5)
hobbies= models.TextField()
To make the "CLOWN" column you see in the image I added this to the clown class:
def __str__(self):
return self.title
I seem to only be able to do this with one attribute/category, in this case it's title. How do I make another column for another attribute, say identification?
in the admin.py file create custom ModelAdmin for your model clown, and register it
from django.contrib import admin
from .models import Clown
class ClownAdmin(admin.ModelAdmin):
list_display = ('title', 'identification', 'hobbies')
admin.site.register(Clown, ClownAdmin)
I created an app with django and python3 containing several tutorials, which in turn contain multiple content entrys that are saved in another table. Each tutorial is represented by an entry in the exercises tutorial model. I want to be able to sort the tutorials and the contents in the django admin panel. It works fine for the inline tutorial contents with SortableInlineAdminMixin class. It also works fine if I create new tutorials from scratch without having any objects saved before (it works local on my pc if I clone this project and set it up from scratch)
My problem now is, that I have the app setup like you see in the code and pictures below on a ubuntu server with apache, but I can't sort the tutorials (the inline contents still work fine). If i drag and drop them to a new position and reload, they don't save at their new position and fall back to their old position.
Tutorial and TutorialContent model:
from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField
# Page for tutorials
class Tutorial(models.Model):
title = models.CharField(max_length=60)
order = models.PositiveIntegerField(default=0, null=False, unique=True)
# Timestamp
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['order']
def __str__(self):
return self.title
# Content for tutorials
class TutorialContent(models.Model):
lesson_page = models.ForeignKey(Tutorial, related_name='tutorial_content', on_delete=models.CASCADE)
content = RichTextUploadingField()
order = models.PositiveIntegerField(default=0, null=False)
# Timestamp
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['order']
def __str__(self):
description = 'Order - ' + str(self.order)
return description
Tutorial and TutorialContent admin:
from django.contrib import admin
from .models import Tutorial, TutorialContent
from adminsortable2.admin import SortableInlineAdminMixin, SortableAdminMixin
class TutorialContentInline(SortableInlineAdminMixin, admin.TabularInline):
model = TutorialContent
class TutorialAdmin(SortableAdminMixin, admin.ModelAdmin):
model = Tutorial
inlines = [TutorialContentInline]
list_display = ['title']
admin.site.register(Tutorial, TutorialAdmin)
Django admin tutorial model panel:
SQLite3 view of the corresponding table:
Maybe a little to late, but:
If there is already some model instance persisted in your db when you add the adminsortable2 field in your model, the ordering will not works, you need to build the order a first time with:
python manage.py reorder your_app.YourModel
Source: https://django-admin-sortable2.readthedocs.io/en/latest/usage.html#initial-data
Hoping this can help.
I'm working on a Django 1.10 & Python 3.6 project, I have deployed it on compute engine successfully with gunicorn.My project is working fine but I have one issue there, I have two models as "Articles" & "TaggedArticle", I have specified all fields from TaggedArticle in admin.py but it doesn't display in Django admin, rather it's displaying all fields in Django admin on my local system. After deployment now I'm using Postgresql instead of SQLite.
Update: Fields are not displaying only in detail view of TaggedArticle in Django Admin
Here are my models:
Article Model:
class Article(models.Model):
link = models.URLField(max_length=255)
category = models.CharField(max_length=255, choices=Categories)
TaggedArticle Model:
class TaggedArticle(models.Model):
user = models.ForeignKey(User, related_name='tagging')
email = models.EmailField(max_length=255)
category_fit = models.CharField(choices=choices, max_length=255)
article = models.ForeignKey(Article, related_name='articles')
link = models.URLField(max_length=255,)
relevant_feedback = models.TextField(blank=True)
category = models.CharField(max_length=255,)
created_at = models.DateTimeField(default=timezone.now, editable=False)
Here's my admin.py for TaggedArticle:
User = get_user_model()
admin.site.unregister(User)
class InlineTaggedArticle(admin.TabularInline):
model = TaggedArticle
class CustomAdmin(UserAdmin):
date_hierarchy = 'date_joined'
inlines = [InlineTaggedArticle, ]
list_display = list(UserAdmin.list_display) + ['totol_tagged_article']
def totol_tagged_article(self, obj):
return obj.tagging.all().count()
admin.site.register(User, CustomAdmin)
class TaggedArticleAdmin(admin.ModelAdmin):
date_hierarchy = 'created_at'
fields = ['category_fit', 'article', 'link', 'relevant_feedback', 'category', 'user', 'email']
list_display = ['link', 'user', 'email']
list_filter = ['user', 'email']
model = Tagged
admin.site.register(Tagged, TaggedArticleAdmin)
Only category_fit & article fields are displaying in Django admin, why other fields don't display inside Django admin? Even it's displaying other fields like link, user & email as you can see I have added these fields in list_display.
Help me, please!
Thanks in advance!
There is an indentation issue, or maybe just typo while writing the question, in your created_at field in TaggedArticle model. Fix this, then apply migrations and check.
Is it possible to do a reverse relation search on the Django Admin interface?
My Django app database schema consists of the following models:
class Tag(models.Model):
title = models.CharField(max_length=50)
class Publication(models.Model):
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True, related_name="publications")
I have added a search field for looking up tags by title in my admin.py file by doing:
class TagAdmin(admin.ModelAdmin):
list_display = ('title',)
search_fields = ('title',)
Thus, when I type a tag title into the search field on the django admin interface, a list of matching tag titles comes up. Now I'd like to make it so that if I type a tag title into the search field, matching publications come up.
In other words, I'm imagining something like:
class TagAdmin(admin.ModelAdmin):
list_display = ('title',)
search_fields = ('publications',)
Which of course doesn't work... but that's the idea...
Is this even possible? And/or am I even going about this the right way? If so, could someone suggest a way to do this or a resource? If you are kind enough to do so, please keep in mind that I am very much a beginner. Thanks.
You shouldn't try to do this using an admin class registered to your Tag model. Instead, set up an admin class for Publication and set its search_fields:
class PublicationAdmin(admin.ModelAdmin):
list_display = ('title',)
search_fields = ('tags__title',)