Home > OS >  'int' object has no attribute 'save' in django
'int' object has no attribute 'save' in django

Time:11-13

I have a value in a database on phpmyadmin and I want to change this value by a new calculated value. The function save() doesn't work and the error tells me that it's because it's an interger. I don't know how to solve this problem.

def DeleteBulle (request, id_bulle):

        #suppression de la bulle par l'id
        id_bulle
        param = Bulles.objects.get(pk=id_bulle)
        #param.delete()

        #adaptation du champ "nombre de bulle" dans la table "site"
        site=param.id_site
        print('site', site)
        compte=Bulles.objects.filter(id_site=site).count()
        print('nombre bulle avec site identique', compte)
        nbrbulle=Bulles.objects.get(pk=id_bulle).id_site.nombre_bulles
        nbrbulle=compte
        nbrbulle.save()
        
        #réussite
        print("Bulle supprimée")

        return redirect('api_bulles_frontend')

Models :

class Site(models.Model):
id_site = models.AutoField(
    db_column="Id_site", primary_key=True
)  # Field name made lowercase.
nom = models.CharField(
    db_column="Nom", max_length=100
)  # Field name made lowercase.
vitesse_b = models.FloatField(db_column="Vitesse_b")  # Field name made lowercase.
vitesse_c = models.FloatField(db_column="Vitesse_c")  # Field name made lowercase.
ecart_type_b = models.FloatField(
    db_column="Ecart_type_b"
)  # Field name made lowercase.
ecart_type_c = models.FloatField(
    db_column="Ecart_type_c"
)  # Field name made lowercase.
type_site = models.CharField(
    db_column="Type_site", max_length=20
)  # Field name made lowercase.
longitude = models.FloatField(db_column="Longitude")  # Field name made lowercase.
latitude = models.FloatField(db_column="Latitude")  # Field name made lowercase.
nombre_bulles = models.IntegerField(db_column="Nombre_bulles")
date_vidange = models.DateField(
    db_column="Date_vidange"
)  # Field name made lowercase.
#trajet = models.ManyToManyField(Trajet, related_name='site_moi')

class Meta:
    db_table = "site"


class Bulles(models.Model):
id_bulle = models.AutoField(primary_key=True)
num_bulle = models.CharField(max_length=20)
type_bulle = models.CharField(max_length=20)
colories = models.CharField(max_length=20)
latitude = models.FloatField()
longitude = models.FloatField()
date_vidange = models.DateField(
    db_column="date_vidange"
) 
id_depot = models.ForeignKey(
    "Depot", on_delete=models.CASCADE, db_column="id_depot"
)
id_site = models.ForeignKey(
    "Site",related_name='bul', on_delete=models.CASCADE, db_column="Id_site"
)  
class Meta:
    db_table = "bulles"

I don't know how to solve this problem. Thank you for your help.

CodePudding user response:

You should save the site object with the updated item, not update the nombre_bulles, which is just an integer:

from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_http_methods


@require_http_methods(['POST', 'DELETE'])
def DeleteBulle(request, id_bulle):
    bulle = get_object_or_404(Bulles, pk=id_bulle)
    site = param.id_site
    bulle.delete()
    site.nombre_bulles = site.bul.count()
    site.save(update_fields=['nombre_bulles'])
    return redirect('api_bulles_frontend')

Note: normally a Django model is given a singular name, so Bulles instead of Bulle.


Note: Normally one does not add a prefix id_… to a ForeignKey field, since Django will automatically add a "twin" field with an …_id suffix. Therefore it should be site, instead of id_site.


Note: It is often better to use get_object_or_404(…) [Django-doc], then to use .get(…) [Django-doc] directly. In case the object does not exists, for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using .get(…) will result in a HTTP 500 Server Error.


Note: One can use the @require_POST decorator [Django-doc] to restrict the view to only be accessible for a POST request.

  • Related