Answer the question
In order to leave comments, you need to log in
How to display filters in Django admin?
Hello. I'm learning django - there was a problem in the process of learning. It was necessary to add filters to the admin.py file as in the lesson, but nothing comes out. Already tried from all sides...
admin.py
from django.contrib import admin
from . import models
admin.site.register(models.Example)
admin.site.register(models.Author)
admin.site.register(models.Book)
admin.site.register(models.Place)
admin.site.register(models.Restaurant)
admin.site.register(models.Waiter)
admin.site.register(models.Publication)
admin.site.register(models.Article)
class AuthorAdmin(admin.ModelAdmin):
list_display = ['name' , 'surname']
# list_display = [field.name for field in models.Author, meta.fields] какая то ошибка в этой строке
# exclude = ['name'] # прячет поле name
# fields = ['name', 'surname'] # показывает поле
# list_filter = ['name']
# search_fields = ['name', 'surname']
# search_fields = [field.name for field in models.Author, meta.fields]
class Meta:
model = models.Author
admin.site.register(models.Author, AuthorAdmin)
from django.db import models
class Example(models.Model):
integer_field = models.IntegerField()
positiv_field = models.PositiveIntegerField()
positive_small_field = models.PositiveSmallIntegerField()
big_integer_field = models.BigIntegerField()
float_field = models.FloatField()
binary_field = models.BinaryField()
boolean_field = models.BooleanField()
char_field = models.CharField(max_length=5)
text_field = models.TextField(max_length=20)
date_field = models.DateField(auto_now=True)
date_time_field = models.DateTimeField(auto_now_add=True)
decimal_field = models.DecimalField(max_digits=8, decimal_places=2)
email = models.EmailField()
file_field = models.FileField(upload_to='file')
image_field = models.ImageField(upload_to='images')
class Author(models.Model):
name = models.CharField(max_length=50, verbose_name = 'Имя')
surname = models.CharField(max_length=50, verbose_name='Фамилия')
date_birth = models.DateField(auto_now=False, verbose_name='Дата рождения')
def __str__(self):
return 'Имя : %s Фамилия : %s' %(self.name, self.surname)
class Meta:
verbose_name = 'Автор'
verbose_name_plural = 'Авторы'
class Book(models.Model):
CHOICE_GENRE = (
('comedy' , 'Comedy'),
('tragedy' , 'Tragedy'),
('drama' , 'Drama'),
)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
title = models.CharField(max_length=50)
text = models.TextField(max_length=1000)
genre = models.CharField(max_length=50, choices=CHOICE_GENRE)
def __str__(self):
return self.title
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
def __str__(self):
return '%s the place' % self.name
class Restaurant(models.Model):
place = models.OneToOneField(Place, on_delete=models.CASCADE)
serves_hot_dog = models.BooleanField(default=False)
serves_pizza = models.BooleanField(default=False)
def __str__(self):
return '%s the restaurant' % self.place.name
class Waiter(models.Model):
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
def __str__(self):
return '%s the waiter at %s' % (self.name, self.restaurant)
class Publication(models.Model):
title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Meta:
ordering = ('title', )
class Article(models.Model):
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication)
def __str__(self):
return self.headline
class Meta:
ordering = ('headline', )
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question