1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Overriding get_queryset method in Django Admin list change without effects

Discussão em 'Python' iniciado por Stack, Setembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    Sorry if I'm repetitive on a widely discussed topic, but I can't figure out where the error is, since debug returns the correct result, but the page in the browser does not. In short, I'm implementing an app that is multi-user and multi-tenant, with the use of the django-organizations module. Each model that identifies particular resources has a 'gruppo' field, Foreign Key to the specific tenant. Furthermore, the session contains the tenant('gruppo_utente') in use, in order to allow the change of one tenant to another simply by changing the data stored in the session.

    request.session['gruppo_utente']=int(tenant) # =gruppo_id


    This is the model:

    //model.py
    from multigroup.models import Gruppo, GruppoUser
    from mptt.models import MPTTModel, TreeForeignKey

    class ContoCOGE (MPTTModel):
    gruppo = models.ForeignKey(Gruppo, on_delete=models.CASCADE, related_name='contoCOCGE_gruppo')
    nome = models.CharField(max_length=2,default='')
    descrizione = models.CharField("Descrizione",max_length=50,default='')
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
    order_insertion_by = ['nome']

    class Meta:
    verbose_name = "Conto"
    verbose_name_plural = "Conti"

    def antenati(self):
    tmp=''
    if self.is_root_node():
    return format_html(f'<b> {{}} - {{}} </b>', mark_safe(self.nome), mark_safe(self.descrizione))
    for i in self.get_ancestors():
    tmp += i.nome + '.'
    return tmp+self.nome+' - '+self.descrizione

    antenati.allow_tags = True

    def __str__(self):
    tmp=''
    if self.is_root_node():
    return format_html(f'<b> {{}} - {{}} </b>', mark_safe(self.nome), mark_safe(self.descrizione))
    for i in self.get_ancestors():
    tmp += i.nome + '.'
    return tmp + self.nome
    __str__.allow_tags = True


    In the admin page then I try to overwrite get_queryset(), with the following definition:

    #admin.py
    class ContoCOGEAdmin (DjangoMpttAdmin):
    def get_queryset(self, request):
    qs = super().get_queryset(request)
    if 'gruppo_utente' in request.session:
    return qs.filter(gruppo_id=int(request.session['gruppo_utente']))
    else:
    return qs

    form = postForm ...
    .... //other stuff


    but the page in the browser always returns the same. This is the data table ContoCOGE:

    [​IMG]

    If gruppo_utente = '3', the sixth row shouldn't be shown, but why:

    [​IMG]

    debugging from vscodium, however, is ok:

    [​IMG]

    where qs.filter(gruppo_id=int(request.session['gruppo_utente'])) returns:

    <TreeQuerySet [<ContoCOGE: <b> 01 - ATTIVO </b>>, <ContoCOGE: 01.10>, <ContoCOGE: <b> 50 - Passivo </b>>, <ContoCOGE: 50.51>, <ContoCOGE: 50.51.01>, <ContoCOGE: <b> 58 - TEMP2 </b>>]>


    Thanks in advance

    Continue reading...

Compartilhe esta Página