On Saturday, December 8, 2018 at 2:44:21 AM UTC+11, Informatico de 
Neurodesarrollo wrote:
>
> Thanks Mike: 
>
> My data are in different formats, but I don't care pass it by hand once 
> time. 
>

Once is enough, I agree.
 

>
> Previously, I know that I need make migrations my model into "postgres" 
> database creating the hole table but empty 
>
> My interest will be learn one way put a previous data into my database, 
> before going on with the develop of my app. 
>

You need a reference list of values you can re-import not just for 
development but for information maintenance in future.

Maybe the government or postal service has a database you can read? Can you 
maybe scrape what you want off the web to build your own list.

I think your models are going to be difficult to deal with if you try to 
manage your own primary keys. For example, in your Provincia class you can 
completely omit idProvincia. If you need a two-character abbreviation for a 
province then put that in and use unique=True
 

>
> eg.: 
>
> In models.py : 
>
> class Provincia(models.Model): 
>      idProvincia = models.CharField(max_length=2, primary_key=true) 
>      ProvinciaName = models.CharField(max_length=30) 
>
>      class Admin:pass 
>
>      def __str__(self): 
>          return self.ProvinciaName 
>
> And the data that I need put into them are: 
>
>
You do not need to manually import and re-import your data directly in 
PostgreSQL. That way madness lies - especially if you try to manage the 
keys yourself. That is what the Django ORM is intended for.
 

> In table: 
>
> Provincia: 
>
>      Insert(idProvincia = 01,  ProvinciaName = "Pinar del Río") 
>
>      Insert(idProvincia = 02,  ProvinciaName = "La Habana") 
>
>      ... 
>

Here is a data import migration I wrote ...

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import, division

from django.db import migrations, models
from refer.utils import import_ecfr_data

class Migration(migrations.Migration):

    dependencies = [
        # edit name of this file and put it into .migrations
        # adjust dependency to last run migration, run migrate and get rid 
of
        # this file
        ('refer', '?previous migration?'),
    ]

    operations = [
        migrations.RunPython(
            # edit refer.utils.import_ecfr_data to return None after it has 
been run
            import_ecfr_data,
        ),
    ]

You can see this template can be re-run as required and the callable 
'import_ecfr_data()' can be edited to switch it off.

import_ecfr_data itself must have arguments like this ...

def import_ecfr_data(apps, schema_editor):

... because the migrations system expects any callable called by 
migrations.RunPython to have those arguments. They are used in your 
callable like this (an example from my project) to fetch your model classes 
...

    Jurisdiction = apps.get_model("regulation", "Jurisdiction")

and

    db_alias = schema_editor.connection.alias

Then my input values exist in a file opened as 'infile' ...

    with open(intext, 'r') as infile:
        juris = 
Jurisdiction.objects.using(db_alias).get(jurisdiction_code=jcode)
        try:
            legis = Regulation.objects.using(db_alias).get(
                                            jurisdiction=juris,
                                            field=field)
        except Regulation.DoesNotExist:
              ... whatever is appropriate

In your case however with Provincia instead of 'get()' you might choose 
'get_or_create()' or perhaps if DoesNotExist occurs you might create one 
separately after processing some checks or reformatting.

In my case I don't ever have to think about key values. Django and Postgres 
do it all for me. Much easier to think like a human being than a relational 
database.

Summarising, assemble your reference data in a file then write an import 
utility and call it from the migration system.

HTH


 

>
> TIA 
> El 06/12/18 a las 16:31, Mike Dewhirst escribió: 
> > Jesús 
> > 
> > Where is your data right now? 
> > 
> > If you can put it into a separate database - or even a spreadsheet - 
> > it can be automatically reloaded at any time. 
> > 
> > There are two ways to do this. One is a data migration ... 
> > 
> > 
> https://docs.djangoproject.com/en/1.11/ref/migration-operations/#runpython 
> > 
> > 
> > ... and the other is a custom manage.py command ... 
> > 
> > 
> https://docs.djangoproject.com/en/1.11/howto/custom-management-commands/#module-django.core.management
>  
> > 
> > 
> > In either case you write a function which uses Django's everyday tools 
> > to insert data. If your data is in a flat file you can read it line by 
> > line and extract the data you want, format it the way you want, run it 
> > through any checks or tests you want then if it doesn't already exist 
> > insert it. 
> > 
> > I first used data migrations to insert reference data and worked out a 
> > way to live with it. In effect, I had to insert 'return None' at a 
> > strategic spot to prevent it running a second time. 
> > 
> > Then I had a very complex data import with lots of m2m data 
> > represented in a flat Excel spreadsheet and that was too ugly for a 
> > data migration so I put in some study time and eventually built a 
> > manage.py command. 
> > 
> > However, a possible third way might be found here ... 
> > 
> > https://djangopackages.org/grids/g/countries/ 
> > 
> > I haven't used any so I can't tell you much. 
> > 
> > Good luck 
> > 
> > Mike 
> > 
> > 
> > On 7/12/2018 5:28 AM, Informatico de Neurodesarrollo wrote: 
> >> 
> >> Saludos Gerson: 
> >> 
> >> Es real, trabajo en salud y estoy trabajando en un sistema para el 
> >> control de medicamentos y necesito tener ya en la base de datos las 
> >> provincias, sus municipios, áreas de salud, medicamentos controlados, 
> >> etc. con los que voy a trabajar y cuando lo termine será puesto es 
> >> todas las farmacias del municipio donde trabajo 
> >> (Cárdenas,Matanzas,Cuba). como decía, estoy comenzando en Django y 
> >> quiero hacerlo porque deseo trabajar sobre software libre y trabajo 
> >> con linux desde el 2004 (más orientado a servidores). 
> >> 
> >> In english: 
> >> 
> >> I am working in Cuban's  system health  care and I am began work on  
> >> a web app apply to a medicine's control and I need put into the 
> >> database to go ahead on my app, some data like all province, each 
> >> municipals  regions, cities and other information (at time to deploy 
> >> my app in production site, I save time and work to introduce a lot of 
> >> data time after time ). 
> >> 
> >> PD Apologist my english and I answered to Gerson's email address. 
> >> PD Disculpen  respondí al correo de Gerson, no a la lista. 
> >> 
> >> El 04/12/18 a las 12:51, Gerson David Vizquel Alemán escribió: 
> >>> Hola Jesus, la información que requieres cargar inicialmente en la 
> >>> base de datos es información fake o real? 
> >>> 
> >>> El martes, 4 de diciembre de 2018, 13:09:04 (UTC-4), Informatico de 
> >>> Neurodesarrollo escribió: 
> >>> 
> >>>     Hello: 
> >>> 
> >>>     I am a beginner on Django. I wish develop a web app with Django 
> >>>     2.1.3 
> >>>     and Postgres. 
> >>> 
> >>>     I have to create my project and later, make migrations on my data 
> >>>     base, 
> >>>     but I need introduce some data previously into my database like 
> all 
> >>>     provinces and other a lot of information's, which don't want 
> >>>     introduce 
> >>>     a lot of data that I want put into them without insert one and 
> >>>     other time. 
> >>> 
> >>>       Any one can help me 
> >>> 
> >>>     -- 
> >>>     Ing. Jesús Reyes Piedra 
> >>>     Admin Red Neurodesarrollo,Cárdenas 
> >>>     La caja decía:"Requiere windows 95 o superior"... 
> >>>     Entonces instalé LINUX. 
> >>> 
> >>> 
> >>>     --     Este mensaje le ha llegado mediante el servicio de correo 
> >>>     electronico que ofrece Infomed para respaldar el cumplimiento de 
> >>>     las misiones del Sistema Nacional de Salud. La persona que envia 
> >>>     este correo asume el compromiso de usar el servicio a tales fines 
> >>>     y cumplir con las regulaciones establecidas 
> >>> 
> >>>     Infomed: http://www.sld.cu/ 
> >>> 
> >>> -- 
> >>> You received this message because you are subscribed to the Google 
> >>> Groups "Django users" group. 
> >>> To unsubscribe from this group and stop receiving emails from it, 
> >>> send an email to django-users...@googlegroups.com <javascript:> 
> >>> <mailto:django-users+unsubscr...@googlegroups.com <javascript:>>. 
> >>> To post to this group, send email to django...@googlegroups.com 
> <javascript:> 
> >>> <mailto:django...@googlegroups.com <javascript:>>. 
> >>> Visit this group at https://groups.google.com/group/django-users. 
> >>> To view this discussion on the web visit 
> >>> 
> https://groups.google.com/d/msgid/django-users/982e6114-a6ea-423c-938c-8865d0652fdd%40googlegroups.com
>  
> >>> <
> https://groups.google.com/d/msgid/django-users/982e6114-a6ea-423c-938c-8865d0652fdd%40googlegroups.com?utm_medium=email&utm_source=footer>.
>  
>
> >>> 
> >>> For more options, visit https://groups.google.com/d/optout. 
> >> 
> >> -- 
> >> 
> >> Ing. Jesús Reyes Piedra 
> >> Admin Red Neurodesarrollo,Cárdenas 
> >> La caja decía:"Requiere windows 95 o superior"... 
> >> Entonces instalé LINUX. 
> >> -- 
> >> You received this message because you are subscribed to the Google 
> >> Groups "Django users" group. 
> >> To unsubscribe from this group and stop receiving emails from it, 
> >> send an email to django-users...@googlegroups.com <javascript:> 
> >> <mailto:django-users+unsubscr...@googlegroups.com <javascript:>>. 
> >> To post to this group, send email to django...@googlegroups.com 
> <javascript:> 
> >> <mailto:django...@googlegroups.com <javascript:>>. 
> >> Visit this group at https://groups.google.com/group/django-users. 
> >> To view this discussion on the web visit 
> >> 
> https://groups.google.com/d/msgid/django-users/bab14959-46fd-9702-6d10-715794bf9c01%40infomed.sld.cu
>  
> >> <
> https://groups.google.com/d/msgid/django-users/bab14959-46fd-9702-6d10-715794bf9c01%40infomed.sld.cu?utm_medium=email&utm_source=footer>.
>  
>
> >> 
> >> For more options, visit https://groups.google.com/d/optout. 
> > 
>
> -- 
>
> Ing. Jesús Reyes Piedra 
> Admin Red Neurodesarrollo,Cárdenas 
> La caja decía:"Requiere windows 95 o superior"... 
> Entonces instalé LINUX. 
>
>
> -- 
> Este mensaje le ha llegado mediante el servicio de correo electronico que 
> ofrece Infomed para respaldar el cumplimiento de las misiones del Sistema 
> Nacional de Salud. La persona que envia este correo asume el compromiso de 
> usar el servicio a tales fines y cumplir con las regulaciones establecidas 
>
> Infomed: http://www.sld.cu/ 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/af82feeb-6415-4cbd-bf7f-96737be4df04%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to