E. Lizbeth E. Lizbeth - 11 months ago 41
Python Question

How do I change the object name of my Django model by the data of a field of my database?

I would like to know how I can change Campaign object and Destino object by data that correspond to my database. I was using code that returns the data to my database but in the part where it shows the actions it does not do that. Is there any other way to do it? Thanks for reading my question and leave your answers.

Here is my admin.py file:

from django.contrib import admin
from .models import Campania, Destino

class CampaniaAdmin(admin.ModelAdmin):
list_display = ('id_campania','nombre_campania','fecha_creacion','fecha_inicio','usuario','correos_diarios','email','sms', 'completado')
search_fields = ['nombre_campania']

class DestinoAdmin(admin.ModelAdmin):
list_display = ('id_destino','unidad_id_unidad','campania_id_campania','enviado','fecha_hora')
search_fields = ['id_destino']

And here is my models.py file:

from __future__ import unicode_literals
from django.db import models

class Campania(models.Model):
id_campania = models.AutoField(primary_key=True, verbose_name='ID')
template = models.ForeignKey('Template', db_column='template', blank=True, null=True, verbose_name='Template')
nombre_campania = models.CharField(max_length=500, blank=True, null=True, verbose_name='Nombre de la campaña')
fecha_creacion = models.DateField(blank=True, null=True, verbose_name='Fecha de creación')
fecha_inicio = models.DateField(blank=True, null=True, verbose_name='Fecha de inicio')
usuario = models.CharField(max_length=250, blank=True, null=True, verbose_name='Creador de la campaña')
correos_diarios = models.IntegerField(blank=True, null=True, verbose_name='Correos por día')
email = models.BooleanField(default=True, verbose_name='Enviar correo')
sms = models.BooleanField(default=True, verbose_name='Enviar mensaje (sms)')
completado = models.NullBooleanField(default=False, blank=True, null=True, verbose_name='Completado')

class Meta:
managed = False
db_table = 'campania'
verbose_name_plural = "Campañas"

def __str__(self):
return '{}'.format(self.nombre_campania)

class Destino(models.Model):
id_destino = models.AutoField(primary_key=True, verbose_name='ID')
unidad_id_unidad = models.ForeignKey('Unidad', db_column='unidad_id_unidad', blank=True, null=True, verbose_name='Unidad destino')
campania_id_campania = models.ForeignKey('Campania', db_column='campania_id_campania', blank=True, null=True, verbose_name='Campaña dirigida')
enviado = models.NullBooleanField(blank=True, null=True, verbose_name='Confirmación de envío de campaña')
fecha_hora = models.DateTimeField(blank=True, null=True, verbose_name='Fecha de envío')

class Meta:
managed = False
db_table = 'destino'
verbose_name_plural = "Destinos"

def __str__(self):
return '{}'.format(self.unidad_id_unidad)

Here is a image of my admin site:
admin site

Check the part of Actions (Acciones)

I'm using version 1.8 of Django and 3.4.4 of Python

Answer Source

Django Admin is using LogEntry to show your recent actions. This model is based on content types - try to run manage.py migrate after changing your models (to change model name in your content type).

Another thing is object name - this is stored in LogEntry in object_repr field and it's dumped right after the action, so changing the way the repr of the model is calculated after performing the action will not be reflected here (it looks that when the action was performed, you haven't defined __str__ for your models and it was using Django's default __str__, which is <Model name> object). If you perform some action (create, update, delete) on particular object right now, you should see here value of object's __str__