[Python] How to assign to ManytoMany None object?

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a task to modify queryset after that a received its from DB and before context. I do the next:

    products = list(qs) #create a copy of qs to avoid modifying in DB
    for product in products:
    access_table = FieldAccess.objects.filter(user=user, product=product.pk).values_list('access', flat=True)
    fields = [Choices.objects.filter(pk=item).values_list('field_name', flat=True) for item in access_table
    if not access_table[0] == None]
    for field in fields:
    setattr(product, field[0], None) #assign a new value to particular field of object
    return products


    It works fine except of ManyToMany Fields. If I try to set None value I get an error:


    'NoneType' object is not iterable
    ​

    What do I have to assign?

    Prehistory
    I need to limit value visibility of particular field items in the model for particular users. I created intermediate model that contain references on user, object of model and which fields should be hidden. I really don't no how to implement that via queryset, but my snippet with modifying on the fly works well

    Continue reading...

Compartilhe esta Página