How to update a float or int field of an entry? - Django

0

Hello friends, sorry for the inconvenience, but I want to update a Float field in my database but I get errors that can not.

My model is as follows:

models.py

class Model1(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    id = models.AutoField(primary_key=True)
    number = models.FloatField(default=0)
    created = models.DateTimeField(auto_now_add=True, null=True)

    class Meta:
        ordering = ['-created']

views.py

I did it in 2 ways: - This is the first one

def actualizar1(request):
    user = request.user
    obj = Model1.objects.all().filter(user=request.user).first()
    number1 = obj.objects.get('number')
    number1 = 20
    number1.save()

And it gives me the following ERROR

  

AttributeError: Manager is not accessible via Model1 instances

Now this is the second one and it gives me the following error:

def actualizar1(request):
    user = request.user
    number1 = Model1.objects.all().filter(user=request.user).first().number
    number1 = 20
    number1.save()

But I get this ERROR:

  

AttributeError: 'float' object has no attribute 'save'

Then I do not know why this happens, thanks for your help!

    
asked by Yamamoto AY 25.08.2018 в 05:48
source

2 answers

1

Try this way:

def actualizar1(request):
       user = request.user
       user_obj = Model1.objects.all().filter(user=request.user).first()
       user_obj.number=20
       user_obj.save()
   ...
    
answered by 25.08.2018 / 21:36
source
0

I know you already have an accepted answer to this question, but I want to answer just to explain what was happening.

In the first way that you tried you had this snippet of code:

obj = Model1.objects.all().filter(user=request.user).first()
number1 = obj.objects.get('number')

Which means that obj has been an instance of class Model1 . The objects attribute is a class attribute, not an instance attribute, so accessing it from the instance throws an error, that's the reason for your first error, when you call the obj instance the objects attribute % that is of the class.

In the second way that you tried you had this other snippet of code:

number1 = Model1.objects.all().filter(user=request.user).first().number
number1 = 20
number1.save()

What you did was: Consult in the database the table called Model1 and among all your records filter the ones from a specific user, get the first one (or the last one, according to your ordering in the Meta of the class) and access its attribute number that when saving it in a variable called number1 you are only saving an object of type int or float according to how Django parsee the data. Therefore, your variable number1 is a numeric object and not an object that you created or that has database methods or attributes as it would be a class that inherits django.db.models.Model . Then in your procedure, you overwrite the variable number1 to 20 and finally call the method save() on an integer, if so, it does not work properly.

So what is the best way?

The best way would be as explained in the other answer:

obj = Model1.objects.filter(user=request.user).first()
obj.number = 20
obj.save()

In this way, when doing the query in the database, we are saving in the variable obj an instance of the class Model1 that does inherit from django.db.models.Model so it has all the attributes and methods of his father, in doing so, we can now directly modify the attribute number of the object, and finally, to save our changes, we call the method save to ensure that they are stored in the database

    
answered by 27.08.2018 в 15:41