[Django]-How to assign items inside a Model object with Django?

44👍

Models are objects, not dictionaries. Set attributes on them directly:

if my_model.is_changed:
    my_model.title = 'something'

Or, if the attribute name is dynamic, use setattr:

attr_name = 'title' # in practice this would be more complex
if my_model.is_changed:
    setattr(my_model, attr_name, 'something')

This changes the in-memory copy of the model, but makes no database changes – for that your attribute would have to be a field and you’d have the call the save method on my_model. You don’t need to do that if you just want to change what the template receives in its context, but just for completeness’s sake:

if my_model.is_changed:
    my_model.title = 'something'
    my_model.save()

Dictionaries are mutable, if you actually have a dictionary:

mydict = {'title': 'foo'}
# legal
mydict['title'] = 'something'

But not everything is a dictionary.

2👍

Yes, you can change values, but this is not how its done. Django Models are Python classes that have models to represent fields. For example a CharField is for holding a string in a database. Let me demonstrate (code from django docs):

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

As you can see above the Python class is a custom Django model. It is linked to a databse, and when you run manage.py syncdb, it will interact with your database to create the tables and columns that you need it to.

Now, in your case:

if my_model.is_changed:
    my_model.title = "Something"
    my_model.save()

0👍

my_model is an object. So, try this:

if my_model.is_changed:
    my_model.title = 'something'
    my_model.save()

0👍

I was using inlineformset_factory, what I had to do was:

Instead of using my_model.title = 'something',

I had to use my_model.instance.title = 'something'


views.py

...

if request.method == "POST":
        formset = modelName2(request.POST, instance=modelName1)
        
        
        
        if formset.is_valid():

            if changeInstance == True:
                models = formset

                # change the title if changeInstance is True
                index = 0
                model = models[index]
                model.instance.title = "something else"
        
                model.save()
        ...

Leave a comment