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

user18004387

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".

Ahtisham

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python: matching values from one list to the sequence of values in another list

Check if a record from one model exists in a list with records from another model in Django

How to take one value from one list and apply it on all values in another list

How to Assign Values from one JSON Object List from Another JSON Object List

One model with 2 categories in Django. How do I display another category from the same model in html?

How to match indexes from one list to another list and store values?

How to move values from one list to another while removing them from the first list

Remove values from one list if they appear in another

Rails - How to call values from one model into another model form

How can I add certain values from one list to another list in .NET

Get the values from one app to another in Django

Subtracts values one list from another in javascript

Display List of objects from another Model Django

How to assign multiple values from one Model into a List<T> in the ViewModel?

How to store values from one model to another model in django?

Find any values from one list in another

How to compare a nested list of Dicts to another nested list of Dicts and update the values in one from the other

How can I list all answers from answer model to my question model in Django ? Relation many to one?

How to validate one field by another in a model in Django?

Update one List from values another List

how to get another one from one list

How to get values from one column where another column's values match a list

Python: How to use a list comprehension to replace Nones in one list with values from another list?

How to set all available values of one model to another model in django?

How do I access image URL from one django model to another? How do I put it on a template?

How to update the value from one model field in to value of another model field in Django?

How can I pick values from one model and use them in another in nodejs using mongoose?

How to swap values from one generic list to another

Django: Retrieve a list of one model property for all distinct values of another property