Genre for models in DJango

Viewed 262

I am creating a site with Django, and I encounter a problem with the administration site.

When I create a new model, I can assign a verbose_name for the singular and plural in the meta class, but I don't find any option that allows me to assign what gender that model is, that is, whether it is feminine (female) or masculine (male).

For example, I have the city model, which in Spanish would be Ciudad and would be feminine. Then when on the admin site I add a new record, the legend Se añadió con éxito el ciudad appears, when the right thing would be Se añadió con éxito la ciudad.

I know this could be a translation issue,

but I suppose something should indicate to the translations that the model should put el or la as the case may be.

Can anybody help me?

2 Answers

Thank you. I have solved the problem using the response received and making some adaptations. Since I already had admin.ModelAdmin classes created, I created a main class that overwrites the response_add method, and I have inherited my ModelAdmin classes from it. Here the code in case it serves someone else.

# admin_custom.py
from django.contrib import admin, messages
class ModelView(admin.ModelAdmin):

    def response_add(self, request, obj):
        msg = 'Registro añadido satisfactoriamente' # Generic message for male and female models
        self.message_user(request, msg, messages.SUCCESS)
        return self.response_post_save_add(request, obj)

# admin.py
from django.contrib import admin, messages
from .models import Pais, Ciudad, ...
from .admin_custom import ModelView
@admin.register(Pais) 
class PaisAdmin(ModelView):
    inlines = [CiudadesInline]

@admin.register(Ciudad) 
class CiudadAdmin(ModelView):
    list_display = ('nombre', 'pais')
    list_filter = ('pais',)
    inlines = [TeatrosInline, ArtistasInline]

...

One solution I can offer is to overwrite the method that generates the message. First we overwrite the method that generates that message: response_add

from django.contrib import admin, messages
class CityView(admin.ModelAdmin):
    def response_add(self, request, obj):
        #you can put your own code here to generate a message
        msg = 'Custom message'
        self.message_user(request, msg, messages.SUCCESS)
        return self.response_post_save_add(request, obj)

Then we indicate that when we create a city use the custom view we create. For this in the file where we registered the models on the admin page we put the following:

from .admin_custom import CityView # assuming the file is called admin_custom.py
admin.site.register(City, CityView)

Edit For add all at once you can do the next

from .admin_custom import CityView
from .models import *
models = (City, Class1, Class2)
admin.site.register(models, CityView)
Related