Hi,
I am looking for a way to display multiple list in the administration nav bar where all of these list will be named differently and show the same model with differents query_set.
I have tried many ways found on the internet but these solutions are 10 years old and doesn’t work anymore
Thanks [KenWhitesell].
I’m sorry English is not my native language I corrected the bad wording, hope you will understand me better.
I have tried this solution before but it doesn’t work.
model.py
class Photo(models.Model):
class Meta:
verbose_name_plural = "des photos"
verbose_name = "une photo"
nom = models.CharField(max_length=30)
categorie = models.ForeignKey(CategoriePhoto, on_delete=models.CASCADE, null=True, blank=True)
fichier = models.ImageField(upload_to='images_prestations/', verbose_name='photo')
mariage = models.BooleanField()
def __str__(self):
"""retourne un object lisible"""
return self.nom
class PhotoMariage(Photo):
class Meta:
proxy = True
verbose_name = "photo de mariage"
admin.py
class PhotoMariageAdmin(admin.ModelAdmin):
def changelist_view(self, request, extra_context=None):
extra_context = {'title': 'Sélectionnez la photo à changer'}
return super(PhotoMariageAdmin, self).changelist_view(request, extra_context=extra_context)
def add_view(self, request, object_id=None, form_url='', extra_context=None):
extra_context = {'title': 'Ajout d\'une photo'}
return super().add_view(request, extra_context=extra_context)
def change_view(self, request, object_id, form_url='', extra_context=None):
extra_context = {'title': 'Modification d\'une photo'}
return super().change_view(request, object_id, form_url, extra_context=extra_context)
# j'utilise get_queryset pour modifier ce qui est affiché dans la liste,
# ici je n'affiche que les photos de mariage
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.filter(mariage=True)
admin.site.register(PhotoMariage, PhotoMariageAdmin)
And what are you getting or not getting with your attempt above? (Is it showing up in the Admin menu? Or is it showing up but not showing the different queryset?)
Endeed I just made an empty test project and it works
model.py
from django.db import models
class Photo(models.Model):
nom = models.CharField(max_length=30)
mariage = models.BooleanField()
def __str__(self):
"""retourne un object lisible"""
return self.nom
class PhotoMariage(Photo):
class Meta:
proxy = True
verbose_name = "photo de mariage"
It depends upon what exactly you’re trying to do. This may or may not be the most appropriate way to do “something”, but it really depends upon what that “something” is. I can’t tell from this snippet what your real objective is.
I understand now !
When i was comeback to my project i was login with superuser account…
The problem was that before I was using a user account that did not have permission to see my new proxy class
So much time spend for nothing…
I’m really sorry to have bothered you for so little, it might be useful for other people who would make the same mistake