Python Forum
Triying to store values from models - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Triying to store values from models (/thread-38588.html)



Triying to store values from models - Larry1888 - Nov-01-2022

Hi all!

Im writing a ini file based on a excel file readed with pandas and the columns writted in the file gonna be different depending on client number, The problem is finding how to store the value of the client number in a variable in my views.py file and use it in a function then be able to use it as a filter when looking for the columns in excel and generate the ini file

if i manually put the value of the client works, but its not the objective

this is the code use to generate the ini file

[b]################# This is the variable that i need from selected record
cliente = 23701 
#################[/b]

def detalles(request):
    concept = get_concept_name()
    max_id = get_max_id()
    path = os.getcwd() + "/CCP"
    if not os.path.isdir(path):
        os.mkdir(path)

    full_name = f"{concept}_FO_{max_id}.ini"
    if os.path.isfile(full_name):
        os.remove(full_name)

    if "GET" == request.method:
        return render(request, "about.html", {})
    else:
        excel_file = request.FILES["excel_file"]

    # Load the xlsx file
    excel_data = pd.read_excel(excel_file)
    # Read the values of the file in the dataframe
    #################
    if cliente = 123456
    #################
        data = pd.DataFrame(
            excel_data,
            columns=[
                "BienesTransp",
                "Descripcion",
                "Cantidad",
                "ClaveUnidad",
                "Unidad",
                "MaterialPeligroso",
                "CveMaterialPeligroso",
                "Embalaje",
                "DescripcionEmbalaje",
                "PesoTotal",
                "Valor",
                "Moneda",
                "FraccionArancelaria",
                "UUIDComercioExt",
                "NumeroPedimento",
            ],
        )
    #################
    elif cliente == 23701:
   #################
        data = pd.DataFrame(
            excel_data,
            columns=[
                "BienesTransp",
                "Descripcion",
                "Cantidad",
                "ClaveUnidad",
                "Unidad",
                "MaterialPeligroso",
                "CveMaterialPeligroso",
                "Embalaje",
                "DescripcionEmbalaje",
                "PesoTotal",
                "Valor",
                "Moneda",
                "FraccionArancelaria",
                "UUIDComercioExt",
                "NumeroPedimento",
            ],
        )
    else:
        print("Ocurrio un error al generar el ini ")
    # Print the content
    data2 = data.fillna("").replace("-", "", regex=True)
    print("The content of the file is:\n", data2)

    # Write the ini file based on columns 
    ini = open(full_name, "w+")
    for i in data2.iterrows():
        ini.write(f"[{i[0]}]\n BienesTransp={i[1][0]}\n Descripcion={i[1][1]}\n Cantidad={i[1][2]}\n ClaveUnidad={i[1][3]}\n Unidad={i[1][4]}\n MaterialPeligroso={i[1][5]}\n CveMaterialPeligroso={i[1][6]}\n Embalaje={i[1][7]}\n CveMaterialPeligroso={i[1][8]}\n PesoEnKg={i[1][9]}\n ValorMercancia={i[1][10]}\n Moneda={i[1][11]}\n FraccionArancelaria={i[1][12]}\n UUIDComercioExt={i[1][13]}\n Pedimentos={i[1][14]}\n ")
    ini.close()  
The user have a table where he can choose wich control want to process so every record have different client information.

This is how the values are displayed in the front ent

[inline]

<!-- Modal -->
<div class="modal fade" id="modelId" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Cargar Mercancias</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">

<form>
<label for="fnumercontrol">Control: {{Controles_privado.numeroControl}}</label><br>
<label for="fcliente">Cliente: {{Controles_privado.claveCliente}}</label><br>
</form>
<br> <br>
<form action = "{% url "detalles" %}" method = "POST" enctype = "multipart/form-data"> {% csrf_token %}
<input type = "file" name = "excel_file" />
<input type = "submit"/>
</form>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>

</div>
</div>
</div>

[/inline]

so i want to filter the columns based on the value that appears in the modal page (see the submit form attachment)

This is the function used to pass the data to the process.html page:

def process(request, numeroControl):
    Controles_privado = Controles.objects.get(numeroControl=numeroControl)
    return render(request, "process.html", {"Controles_privado": Controles_privado})
Any tips to reach this, is the only things that is left to finish the proyect :s


RE: Triying to store values from models - Pedroski55 - Nov-02-2022

No te entiendo bien, pero:

You could try a dictionary: key = client number, value = a list of column names for that client

{'client number1', ['column name 1', 'column name 2', 'column name 3', ...... , 'last column'], 'client number2', ['column name 1', 'column name 2', 'column name 3', ...... , 'last column']}