Django公司模型.objects.all不显示信息

2024-04-25 20:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用django制作一个webapp,我正在尝试学习如何进行查询。你知道吗

我已经将SQLServer数据库连接到django,并从sqlmanagementstudio添加了我拥有的3个表的信息。然后我运行python manage.py inspectdb,把结果复制到我的blog.models.py,然后把python manage.py makemigrationsmigrate

但是,当我通过python manage.py shell打开python shell时,导入模型并生成`模型.objects.all(),结果如下:

<QuerySet [<Usuario: Usuario object (1)>, <Usuario: Usuario object (2)>]>

Django指出我创建了2个对象,但每当我试图从其中一个对象获取特定信息时,它会显示以下内容:

>>> Usuario.objects.all()
   <QuerySet [<Usuario: Usuario object (1)>, <Usuario: Usuario object (2)>]>
>>> user= Usuario.objects.all()
>>> user[0]
   <Usuario: Usuario object (1)>
>>> user[0].ID_Usuario
   Traceback (most recent call last):
   File "<console>", line 1, in <module>
   AttributeError: 'Usuario' object has no attribute 'ID_Usuario'
>>>     

这是我通过sqlmanagementstudio插入的信息

INSERT INTO Usuario(ID_Usuario,Nombre,Apellido,Edad,Email,Password,ID_Cargo,ID_Rol)
     VALUES(1,'Pepe','Arana',36,'pepe@hotmail.com','pass',1,1),
     (2,'Roquito','Velarde',40,'roquito@hotmail.com','pass',2,1)

为什么django没有识别属性,但是它识别了一个对象已经被创建的事实?你知道吗

谢谢

编辑:这是模型中的模型博客.model.py文件

class Cargo(models.Model):
     id_cargo = models.IntegerField(db_column='ID_Cargo', primary_key=True)  # Field name made lowercase.
     nombre = models.CharField(db_column='Nombre', max_length=100, blank=True, null=True)  # Field name made lowercase.
     descripcion = models.CharField(db_column='Descripcion', max_length=100, blank=True, null=True)  # Field name made lowercase.

     class Meta:
         managed = False
         db_table = 'Cargo'


 class Rol(models.Model):
     id_rol = models.IntegerField(db_column='ID_Rol', primary_key=True)  # Field name made lowercase.
     nombre = models.CharField(db_column='Nombre', max_length=100, blank=True, null=True)  # Field name made lowercase.
     descripcion = models.CharField(db_column='Descripcion', max_length=100, blank=True, null=True)  # Field name made lowercase.

     class Meta:
         managed = False
         db_table = 'Rol'


 class Usuario(models.Model):
     id_usuario = models.IntegerField(db_column='ID_Usuario', primary_key=True)  # Field name made lowercase.
     nombre = models.CharField(db_column='Nombre', max_length=100, blank=True, null=True)  # Field name made lowercase.
     apellido = models.CharField(db_column='Apellido', max_length=100, blank=True, null=True)  # Field name made lowercase.
     edad = models.IntegerField(db_column='Edad', blank=True, null=True)  # Field name made lowercase.
     email = models.CharField(db_column='Email', max_length=100, blank=True, null=True)  # Field name made lowercase.
     password = models.CharField(db_column='Password', max_length=100, blank=True, null=True)  # Field name made lowercase.
     id_cargo = models.ForeignKey(Cargo, models.DO_NOTHING, db_column='ID_Cargo', blank=True, null=True)  # Field name made lowercase.
     id_rol = models.ForeignKey(Rol, models.DO_NOTHING, db_column='ID_Rol', blank=True, null=True)  # Field name made lowercase.

     class Meta:
         managed = False
         db_table = 'Usuario'

Tags: nameidtruefielddbmodelscolumnnull