admin.py
from django.contrib import admin
from .models import Bd, Category
class AdminBd(admin.ModelAdmin):
list_display = ('name', 'content')
filter_horizontal = ['category', ]
admin.site.register(Bd, AdminBd)
admin.site.register(Category)
models.py
from django.db import models
from django.urls import reverse
class Category(models.Model):
title = models.CharField(max_length=50, verbose_name='Категория')
def __str__(self):
return self.title
def get_absolute_ulr(self):
return reverse('category', kwargs={"category_id": self.pk})
class Meta:
verbose_name = 'Категория'
verbose_name_plural = 'Категории'
ordering = ['title', ]
class Bd(models.Model):
name = models.CharField(max_length=50, verbose_name='Имя')
content = models.TextField(max_length=1500, verbose_name='Именование')
category = models.ManyToManyField('Category', verbose_name='Категория')
photo = models.ImageField(upload_to='photo/%Y/%m/%d', blank=True, null=True, verbose_name='Картинка')
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Дата публикации')
updated_at = models.DateTimeField(auto_now=True, verbose_name='Обновлено')
is_created = models.BooleanField(default=False, verbose_name='Создан мной')
def __str__(self):
return self.name
def get_absolute_ulr(self):
return reverse('view_news', kwargs={"news_id": self.pk})
class Meta:
verbose_name = 'Объявление'
verbose_name_plural = 'Объявления'
ordering = ['-updated_at', '-created_at']