Python Forum
Triying to store values from models
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Triying to store values from models
#1
Question 
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

Attached Files

Thumbnail(s)
       
Reply
#2
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']}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  store all variable values into list and insert to sql_summary table mg24 3 1,166 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Python3 requests.models.Response ogautier 4 5,432 Feb-17-2022, 04:46 PM
Last Post: ogautier
  How Do I Download Models Using PIP? CopBlaster 3 2,625 May-18-2020, 08:48 PM
Last Post: hussainmujtaba
  Multivariate mixed effects models primeprover 1 2,596 Aug-19-2018, 01:14 PM
Last Post: ketanm
  view_db.models [HELP] Lartos 0 1,912 Jun-14-2018, 03:33 PM
Last Post: Lartos
  creating a blog, models.py is empty rickadams 3 3,522 Nov-11-2017, 03:51 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020