Home > database >  django framework: get value from django.db.models.fields.IntegerField
django framework: get value from django.db.models.fields.IntegerField

Time:04-02

How can I get value from class django.db.models.fields.IntegerField? I want get 1 from admin field. This is python code: model class and function view

from django.db import models
from django.db import connection
import pymysql
    
# Create your models here.

class Users(models.Model):
    def __init__(self,*args, **kwargs):
        super().__init__(*args, **kwargs)
        self.firstname=models.CharField(max_length=100)
        self.lastname=models.CharField(max_length=100) 
        self.username=models.CharField(max_length=50)
        self.password=models.CharField(max_length=255)
        self.admin=models.IntegerField()
        self.createdAt=models.DateTimeField(db_column="created_at")

    def getAdmin(self):
        return self.admin

    class Meta:
        db_table = 'users'

A view function when I run user = Users.objects.get(id=userId)

def home(request):    
    #controllo se sei loggato
    if request.session.has_key('loggedin'):
        if request.session['loggedin'] != True:
            return redirect("login")
    else:
        return redirect("login")
    
    #l'utente è loggato recupero le informazioni
    userId = int(request.session['id'])
    user = Users.objects.get(id=userId) 
    print(type(user))
    print(type(user.getAdmin()))
    tmplVar = {}
    tmplVar["admin"] = user.admin
    return render(request, 'pygiustizia/home.html',{'tmplVar': tmplVar})

CodePudding user response:

you are printing the type not the value

print(type(user))
print(type(user.getAdmin()))

if you try something like:

print(user)
print(user.admin)

you should get something on the note of:

>> <object>
>> 1

unless you redefine __repr__ or __str__

CodePudding user response:

I presume, you want the value of admin. From my understanding what you can do is you can use filter method on you object inside your Users model. As you have intialized the model fields inside the __init__ method, that I am sure is not a good practice, but based on your requirement here you can do:

from django.db import models
    
# Create your models here.

class Users(models.Model):
    def __init__(self,*args, **kwargs):
        super().__init__(*args, **kwargs)
        self.firstname=models.CharField(max_length=100)
        self.lastname=models.CharField(max_length=100) 
        self.username=models.CharField(max_length=50)
        self.password=models.CharField(max_length=255)
        self.admin=models.IntegerField()
        self.createdAt=models.DateTimeField(db_column="created_at")

    def getAdmin(self):
        return self.objects.get(admin=self.admin)

    class Meta:
        db_table = 'users'

Then you can replace your line user = Users.objects.get(id=userId) with user = User(userId)

I hope this is what you are looking for!

CodePudding user response:

I'm confused. I have commented super and __init__ and it works. Why first time no?

from django.db import models
from django.db import connection
import pymysql
   
# Create your models here.

class Users(models.Model):

    firstname=models.CharField(max_length=100)
    lastname=models.CharField(max_length=100) 
    username=models.CharField(max_length=50)
    password=models.CharField(max_length=255)
    admin=models.IntegerField()
    createdAt=models.DateTimeField(db_column="created_at")

    def getAdmin(self):
        return self.admin

I want initialize all column field in init.

  • Related