from django.contrib import admin
from .models import Category, Article

class CategoryAdmin(admin.ModelAdmin):
    readonly_fields = ('created_at', )
    search_fields = ('title', 'content')
    list_display = ('name','created_at')

class ArticleAdmin(admin.ModelAdmin):
    readonly_fields = ('user','created_at', 'updated_at')
    search_fields = ('title', 'content','user__username', 'categories__name')
    list_display = ('title',  'user', 'public','created_at')
    list_filter = ('public', 'user__username', 'categories__name')

    def save_model(self, request, obj, form, change):
        if not obj.user_id:
            obj.user_id = request.user.id

        obj.save()

# ---- Fin de ArticleAdmin


# Register your models here.

admin.site.register(Category, CategoryAdmin)
admin.site.register(Article, ArticleAdmin)
