Search code examples
djangomodeladditiontransfer

How to add/transfer the values list from one model to another in django?


Let me show you what I have done successfully and what I am trying to achieve now.

I have 2 models. Both has same 3 fields. Name, Month, Number. Two models are Main and BMO.

I filter the first model based on name and month and get the values list of the number column. Code looks like this-

result= Main.objects.filter(name="John", month='apr').values_list('number', flat=True)

Then I display that on django template with a for loop and conditional. Code looks like this -

<p>result:
    <span>
        {% for i in result %}
            {{i}}{% if not forloop.last %}+{% endif %}
        {% endfor %}

    </span>
</p>


result = 2+4+12+7+18

The last line showing you how it actually looks on the template. You can see I display it with + signs between the values.

What I want to achieve:

I want to get the sames values list but instead of showing it on the template I want to insert it to another model's field. Just to be clear I want all the values with plus sign included (the way it looks on my template) to be inserted in a single field of the second model BMO. If I have 10 values they should all be joined by plus sign and added in a single field, not spread into 10 rows of a column.

HERE IS WHAT I TRIED:

I filtered the second model BMO based on name and month and tried updating the number with .update() method. I added the following line of code in views.py right below the first line of code. I am not using the template at all.

result= Main.objects.filter(name="John", month='apr').values_list('number', flat=True)

Bmo.objects.filter(name="test", month='apr).update(number = result)

It didn't change or add anything to it. So I tried a new line of code with a for loop like this -

result= Main.objects.filter(name="John", month='apr').values_list('number', flat=True)

for i in result:
    Bmo.objects.filter(name__icontains = 'test').update(number = i)

This also didn't change a thing. There is only 1 row with name 'test' so I didn't bother with month filter. Wasn't totally convinced with the for loop but I had successfully updated categories with name match with this exact code before so thought this might work. The difference is the category was a single string like category='personal' instead of a variable or multiple variables as in this case.

I think I am very close to achieving what I want to , within a line or two codes. If there is a simpler way of doing what I am trying to do, I am all ears. Thank you for any input.

UPDATE:

After getting the answer from Ahtisham I updated the code. But it hasn't changed anything unfortunately. I am trying it within the same model. Nothing breaks. No errors in console. I run the code and nothing happens. Here is how the main part of views.py looks:

def index(request):
    form = BmoForm()
    if request.method == 'POST':
        form = BmoForm(request.POST)
        if form.is_valid():
            form.save()
    R = Bmo.objects.all()

    var="apr"
    result = Bmo.objects.filter(name="Transfer", month=var).values_list('number', flat=True)
    number = str()
    count = len(products1)
    new = 12+3
    for index, r in enumerate(products1):
        number +=str(r) + '+' if count != index+1 else str(r)
    Bmo.objects.filter(name = "test", month = "apr").update(number = number)  

The variable "new" is there for test only. When I hard code the variable to update with, it works. But it won't update with variable "number".


Solution

  • You can do it like this:

    result = Main.objects.filter(
       name="John", month='apr'
    ).values_list('number', flat=True)
    
    number = str()
    count = len(result)
    
    for index, r in enumerate(result):
      number += str(r) + '+' if count != index + 1 else str(r)
    
    Bmo.objects.filter(name="test", month='apr').update(number = number)