Luis Borbolla
Luis Borbolla

Reputation: 73

Django insert model foreign key

Hi I'm trying to populate my django app with data from a dbf file , I'm trying to make objects , as taught in the 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()

How can I add foreign keys and many to many keys this way ?

Or probably a better approach? dbf files have thousands of rows , so updating data by hand wouldn't be a viable approach.

Here's my models.py as suggested , almoust every model includes a foreign key , or a many to many field , I'm kind of stuck , because of filling them , I'm using dbf2py library to read the foxpro databases, and want to make a script for exporting the data

thanks in advance

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)

Upvotes: 2

Views: 1421

Answers (1)

Transformer
Transformer

Reputation: 3760

try and include your models.py, while you do that take a look at One-toMany for Many-to-many relationship

When you define a relationship in a model (i.e., a ForeignKey, OneToOneField, or ManyToManyField), instances of that model will have a convenient API to access the related object(s).

Using the models for example, an Entry object e can get its associated Blog object by accessing the blog attribute: e.blog.

(Behind the scenes, this functionality is implemented by Python descriptors. This shouldn’t really matter to you)

Django also creates API accessors for the “other” side of the relationship – the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entry objects via the entry_set attribute: b.entry_set.all().

This is directly from the link i gave above, so visit the link and read deep

If a model has a ForeignKey, instances of that model will have access to the related (foreign) object via a simple attribute of the model.

Example:

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

You can get and set via a foreign-key attribute. As you may expect, changes to the foreign key aren’t saved to the database until you call save(). Example:

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

Upvotes: 1

Related Questions