[Python] How to change/filter option list in TreeNodeChoiceField in a TabularInline

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

  1. Stack

    Stack Membro Participativo

    How to change the option/choice list of conto field, based on value gruppo_id?

    conto is a ForeignKey to a branch of a tree described as model.ContoCOGE(MPTTModel). For each gruppo there is a tree, which develops to a certain depth. How to filter among all the trees and, in TreeNodeChoiceField in a TablularInline, to list only that with a group_id?

    In debug, formfield_for_foreignkey(self, db_field, request, **kwargs) overriden returns the correct tree. But the browser renders the page with the select field with all the trees.

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

    class ContoCOGE (MPTTModel):
    gruppo = models.ForeignKey(Gruppo, on_delete=models.CASCADE,related_name='contoCOCGE_gruppo')
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class PncGen (models.Model):
    gruppo = models.ForeignKey(Gruppo, on_delete=models.CASCADE)
    #other stuff

    class PncRighe (models.Model):
    gruppo = models.ForeignKey(Gruppo, on_delete=models.CASCADE)
    idPnc = models.ForeignKey(PncGen,on_delete=models.CASCADE) #puntatore alla testata
    conto = models.ForeignKey(ContoCOGE,on_delete=models.CASCADE,blank=True, null=True)
    #other stuff


    #admin.py
    from django_mptt_admin.admin import DjangoMpttAdmin

    class PncRigheInline(admin.TabularInline):
    model = PncRighe
    autocomplete_fields = ['partitarioRiga','conto']
    extra = 1

    def filter_tree_queryset(self, queryset,request):
    qs = queryset.filter(gruppo_id=int(request.session['gruppo_utente']))
    return qs

    def get_changeform_initial_data(self, request):
    return {'gruppo': int(request.session['gruppo_utente'])}

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'gruppo' or db_field.name == 'idRiga' or db_field.name == 'idPnc':
    kwargs['widget'] = forms.HiddenInput()

    if db_field.name == 'conto':

    db = kwargs.get("using")
    qs = self.get_field_queryset(db, db_field, request)
    kwargs['queryset']=qs.filter(gruppo_id = request.session['gruppo_utente'] )

    @admin.register(PncGen)
    class PncGenAdmin(admin.ModelAdmin):
    #...
    inlines = (PncRigheInline,)

    Continue reading...

Compartilhe esta Página