Django插入模型外键

时间:2016-02-14 17:34:39

标签: django python-2.7 django-models

您好我尝试使用dbf文件中的数据填充我的django应用程序,我试图制作对象,如djangobook所述

>>> p = Publisher(name='Apress',
address='2855 Telegraph Ave.',
city='Berkeley',
state_province='CA',
country='U.S.A.',
website='http://www.apress.com/')

>>> p.save()

如何以这种方式添加外键和多对键?

或者可能是更好的方法? dbf文件有数千行,因此手动更新数据不是一种可行的方法。

这里是我建议的models.py,因为每个模型都包含一个外键,或者很多很多字段,我有点卡住,因为填充它们,我使用dbf2py库用于读取foxpro数据库,并希望创建用于导出数据的脚本

提前致谢

from __future__ import unicode_literals

from django.db import models

class Terminos_pago(models.Model):
    terminos = models.CharField(max_length = 20)

    def __unicode__(self):
        return self.terminos



class Clientes(models.Model):
    """docstring for Clientes"""
    nombre    = models.CharField(max_length=40)
    direccion = models.CharField(max_length=70)
    estado    = models.CharField(max_length=16)
    ciudad    = models.CharField(max_length=30)
    cp        = models.IntegerField()
    kilometros= models.IntegerField()
    rfc       = models.CharField(max_length=13 , null = True)
    horas     = models.DecimalField(null = True,decimal_places = 2 , max_digits = 5)
    terminos_pago = models.ForeignKey(Terminos_pago,null=True)
    dias_de_credito = models.IntegerField(blank = True , null = True)

    def __unicode__(self):
        return u'%s %s' % (self.nombre , self.horas)

class Contactos(models.Model):
    """docstring for Contactos"""
    nombre       = models.CharField(max_length=30)
    departamento = models.CharField(max_length=16)
    telefono     = models.CharField(max_length = 16)
    extension    = models.IntegerField()
    email        = models.EmailField(blank = True)
    cliente      = models.ForeignKey(Clientes)

    def __unicode__(self):
        return self.nombre    


class Maquinas(models.Model):
    """docstring for Maquinas"""
    contacto        = models.ForeignKey(Contactos , null = True)
    id_usuario     = models.CharField(max_length=13 , null = True , blank = True)
    fabricante     = models.CharField(max_length=15 )
    no_serie       = models.CharField(max_length=10 )
    modelo         = models.CharField(max_length=10 )
    rango_x        = models.IntegerField()
    rango_y        = models.IntegerField()
    rango_z        = models.IntegerField()
    mppl           = models.IntegerField()
    mppe           = models.IntegerField()
    probe_type     = models.CharField(max_length=10 )
    probe_head     = models.CharField(max_length=16)
    probe_serial   = models.CharField(max_length=15 )
    extension      = models.IntegerField( blank = True , null = True)
    version_software=models.CharField(max_length=15)
    version_firmware=models.CharField(max_length=15)
    controlador    = models.CharField(max_length=10)
    accesorios     = models.CharField(max_length=15 , null = True , blank = True)
    driver_software= models.CharField(max_length=15)
    modelo_computadora=models.CharField(max_length=10)
    fecha_fabricacion = models.DateField(blank=True , null = True)
    diametro_stylus= models.IntegerField()

    def __unicode__(self):
        return u'%s %s %s %s ' % (self.modelo , self.fabricante , self.contacto.nombre , self.contacto.cliente.nombre)

class Servicios(models.Model):
    """docstring for Servicios"""
    servicio = models.CharField(max_length = 20)

    def __unicode__(self):
        return self.servicio

class ListaPrecios(models.Model):
    """docstring for ListaPrecios"""
    fecha          = models.DateField(null = True)
    horas          = models.IntegerField()
    horas_extra    = models.IntegerField()
    horas_viaje    = models.IntegerField(null = True)
    kilometros     = models.IntegerField()
    hotel          = models.IntegerField()
    manuales       = models.IntegerField()
    traslados      = models.IntegerField()
    avion          = models.IntegerField()
    sobre_equipaje = models.IntegerField() 
    renta_auto     = models.IntegerField()
    papeleria      = models.IntegerField()

    def __unicode__(self):
        return str(self.fecha)



class Ingenieros(models.Model):
    """docstring for Ingenieros"""
    nombre      = models.CharField(max_length=20)
    referencia = models.CharField(max_length=4)
    telefono   = models.CharField(max_length = 16)
    email      = models.EmailField(null = True)         

    def __unicode__(self):
        return self.nombre

class Cotizacion(models.Model):
    """docstring for Cotizacion"""
    fecha          = models.DateField()
    contacto       = models.ForeignKey(Contactos , null = True)
    servicio       = models.ManyToManyField(Servicios)
    maquinas       = models.ManyToManyField(Maquinas)
    horas          = models.IntegerField()
    horas_extra    = models.IntegerField(blank=True ,null = True)
    #horas_viaje    = models.IntegerField()
    viajes         = models.IntegerField()
    hotel          = models.IntegerField(blank=True ,null = True)
    manuales       = models.IntegerField(blank=True ,null = True)
    traslados      = models.IntegerField( blank=True ,null = True)
    aviones        = models.IntegerField(blank=True ,null = True)
    sobre_equipaje = models.IntegerField(blank=True ,null = True)
    renta_auto     = models.IntegerField(blank=True ,null = True)
    papeleria      = models.IntegerField(blank=True ,null = True)
    importe        = models.IntegerField(blank = True , null = True)
    iva            = models.DecimalField(decimal_places = 2 , max_digits = 5 ,blank = True , default = 0.16)
    observaciones  = models.CharField(blank=True ,max_length = 255, null = True)
    SA             = models.NullBooleanField()
    tipo_cambio    = models.DecimalField(decimal_places = 2 , max_digits = 5, blank = True , null = True)

    def __unicode__(self):
        return u'%s %s %s %s' % (self.fecha , self.contacto.cliente.nombre , self.contacto.nombre ,self.servicio)

class Ordenes_de_servicio(models.Model):
    """docstring for Ordenes_de_trabajo"""
    fecha              = models.DateField(null = True)
    ingeniero          = models.ManyToManyField(Ingenieros) 
    observaciones      = models.CharField(max_length = 255,null = True , blank = True)
    viaticos           = models.IntegerField()
    orden_compra       = models.CharField(max_length = 15)
    orden_compra_interna = models.IntegerField(blank = True , null = True)    
    fecha_servicio     = models.DateField(null = True)
    viaticos_pagados   = models.NullBooleanField()
    cotizacion         = models.ForeignKey(Cotizacion,null = True)
    mail_enviado       = models.IntegerField(null=True,blank=True,default=0)
    fecha_mail_enviado = models.DateField(null=True , blank = True)
    contacto_servicio  = models.ForeignKey(Contactos , null = True )




    def __unicode__(self):
        return u'%s %s' % (self.fecha,self.ingeniero)

class Factura(models.Model):
    """docstring for Factura"""
    fecha = models.DateField()    
    orden_servicio = models.ForeignKey(Ordenes_de_servicio)
    descripcion    = models.CharField(max_length=255,null = True , blank = True)
    pagada         = models.NullBooleanField()


    def __unicode__(self):
        return u'%s %s %s' % (self.orden_servicio.cotizacion.contacto.cliente.nombre , self.orden_servicio , self.fecha)

1 个答案:

答案 0 :(得分:1)

尝试包含您的models.py,当您这样做时,请One-toMany查看Many-to-many relationship

在模型中定义关系时(即ForeignKey,OneToOneField或ManyToManyField),该模型的实例将具有访问相关对象的便捷API。

例如,使用模型,Entry对象e可以通过访问博客属性e.blog来获取其关联的Blog对象。

(在幕后,这个功能是由Python描述符实现的。这对你来说并不重要)

Django还为关系的“其他”方创建API访问器 - 从相关模型到定义关系的模型的链接。例如,Blog对象b可以通过entry_set属性访问所有相关Entry对象的列表:b.entry_set.all()

这是直接来自我上面给出的链接,所以请访问该链接并深入阅读

如果模型具有ForeignKey,则该模型的实例将通过模型的简单属性访问相关(外部)对象。

示例:

>>> e = Entry.objects.get(id=2)
>>> e.blog # Returns the related Blog object.

您可以通过外键属性获取和设置。正如您所料,在调用save()之前,对外键的更改不会保存到数据库中。例如:

>>> e = Entry.objects.get(id=2)
>>> e.blog = some_blog
>>> e.save()