Django模型和连接sql表

时间:2017-09-26 10:02:31

标签: python sql-server django django-models

我想尝试使用django而不必在数据库上调用我的存储过程。

所以我创建了一个新模型,从两个不同的模型中获取信息

class TbMouvementinit(models.Model):
    id = models.AutoField(db_column='Id', primary_key=True)  # Field name made lowercase.
    dateheurecreat = models.TextField(db_column='DateHeureCreat')  # Field name made lowercase.
    dateheureclot = models.TextField(db_column='DateHeureClot', blank=True, null=True)  # Field name made lowercase.
    id_pesee = models.BigIntegerField(db_column='Id_Pesee')  # Field name made lowercase.
    id_proteine = models.BigIntegerField(db_column='Id_Proteine', blank=True, null=True)  # Field name made lowercase.
    id_humidite = models.BigIntegerField(db_column='Id_Humidite', blank=True, null=True)  # Field name made lowercase.
    id_espece = models.BigIntegerField(db_column='Id_Espece', blank=True, null=True)  # Field name made lowercase.
    id_produit = models.BigIntegerField(db_column='Id_Produit', blank=True, null=True)  # Field name made lowercase.
    id_codeechantillon = models.BigIntegerField(db_column='Id_CodeEchantillon', blank=True, null=True)  # Field name made lowercase.
    id_traitement = models.BigIntegerField(db_column='Id_Traitement', blank=True, null=True)  # Field name made lowercase.
    id_circuit = models.BigIntegerField(db_column='Id_Circuit', blank=True, null=True)  # Field name made lowercase.
    code_source = models.CharField(db_column='Code_Source', max_length=20, blank=True, null=True)  # Field name made lowercase.
    code_destination = models.CharField(db_column='Code_Destination', max_length=20, blank=True, null=True)  # Field name made lowercase.
    nomos = models.CharField(db_column='NomOS', max_length=30, blank=True, null=True)  # Field name made lowercase.
    codesite = models.CharField(db_column='CodeSite', max_length=9, blank=True, null=True)  # Field name made lowercase.
    type_mouvement = models.CharField(db_column='Type_Mouvement', max_length=3, blank=True, null=True)  # Field name made lowercase.
    sous_domaine = models.CharField(db_column='Sous_Domaine', max_length=1, blank=True, null=True)  # Field name made lowercase.
    recolte = models.SmallIntegerField(db_column='Recolte', blank=True, null=True)  # Field name made lowercase.
    espece = models.CharField(db_column='Espece', max_length=9, blank=True, null=True)  # Field name made lowercase.
    code_variete = models.CharField(db_column='Code_Variete', max_length=10, blank=True, null=True)  # Field name made lowercase.
    code_tiers = models.CharField(db_column='Code_Tiers', max_length=9, blank=True, null=True)  # Field name made lowercase.
    num_bl_livreur = models.CharField(db_column='Num_Bl_Livreur', max_length=9, blank=True, null=True)  # Field name made lowercase.
    num_contrat_client = models.IntegerField(db_column='Num_Contrat_Client', blank=True, null=True)  # Field name made lowercase.
    num_oe = models.IntegerField(db_column='Num_OE', blank=True, null=True)  # Field name made lowercase.
    code_transporteur = models.CharField(db_column='Code_Transporteur', max_length=9, blank=True, null=True)  # Field name made lowercase.
    immat_transporteur = models.CharField(db_column='Immat_Transporteur', max_length=10, blank=True, null=True)  # Field name made lowercase.
    poids_charge = models.FloatField(db_column='Poids_Charge', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    poids_vide = models.FloatField(db_column='Poids_Vide', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract1 = models.CharField(db_column='Code_Caract1', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract1 = models.FloatField(db_column='Val_Caract1', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract2 = models.CharField(db_column='Code_Caract2', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract2 = models.FloatField(db_column='Val_Caract2', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract3 = models.CharField(db_column='Code_Caract3', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract3 = models.FloatField(db_column='Val_Caract3', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract4 = models.CharField(db_column='Code_Caract4', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract4 = models.FloatField(db_column='Val_Caract4', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract5 = models.CharField(db_column='Code_Caract5', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract5 = models.FloatField(db_column='Val_Caract5', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract6 = models.CharField(db_column='Code_Caract6', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract6 = models.FloatField(db_column='Val_Caract6', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract7 = models.CharField(db_column='Code_Caract7', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract7 = models.FloatField(db_column='Val_Caract7', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract8 = models.CharField(db_column='Code_Caract8', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract8 = models.FloatField(db_column='Val_Caract8', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract9 = models.CharField(db_column='Code_Caract9', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract9 = models.FloatField(db_column='Val_Caract9', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_caract10 = models.CharField(db_column='Code_Caract10', max_length=5, blank=True, null=True)  # Field name made lowercase.
    val_caract10 = models.FloatField(db_column='Val_Caract10', blank=True, null=True)  # Field name made lowercase. This field type is a guess. Modified
    code_cel = models.CharField(db_column='Code_Cel', max_length=5, blank=True, null=True)  # Field name made lowercase.
    num_operation = models.IntegerField(db_column='Num_Operation', blank=True, null=True)  # Field name made lowercase.
    datetime2supv = models.CharField(db_column='DateTime2SUPV', max_length=20, blank=True, null=True)  # Field name made lowercase.
    num_lot_supv = models.BigIntegerField(db_column='Num_Lot_SUPV', blank=True, null=True)  # Field name made lowercase.
    user_supv = models.CharField(db_column='User_SUPV', max_length=18, blank=True, null=True)  # Field name made lowercase.


class TbEspece(models.Model):
    id = models.AutoField(db_column='Id', primary_key=True)  # Field name made lowercase.
    recolte = models.SmallIntegerField(db_column='Recolte')  # Field name made lowercase.
    groupe = models.CharField(db_column='Groupe', max_length=3)  # Field name made lowercase.
    categorie = models.CharField(db_column='Categorie', max_length=3)  # Field name made lowercase.
    code = models.CharField(db_column='Code', max_length=3)  # Field name made lowercase.
    libelle = models.CharField(db_column='Libelle', max_length=30)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'TB_Espece'
        app_label = 'erp'

class ResumeMvtActif(models.Model):
    mvt = models.ForeignKey(TbMouvementinit)
    espece = models.ForeignKey(TbEspece)

然后使用

调用它
mouvements = ResumeMvtActif.objects.all()

但当然我收到数据库错误,因为我没有真正的ResumeMvtActif表。

我该如何继续做这项工作?我必须在我的数据库上创建一个额外的表吗?有没有诀窍告诉Django它不是真正的表,只是两个表的连接?

0 个答案:

没有答案