Thursday, June 29, 2017

Re: Pre-loading static data into database table

Hi Rich,

419 is not that many.  I'd probably do that via a Python shell.  import models.Cities  for item in lists_of_lists_of_419  temp = Cities(city+name=item[0], county_name=item[1]) temp.save()


John Thomas
310-947-8572

On Thu, Jun 29, 2017 at 11:16 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
  Reading
<https://docs.djangoproject.com/en/1.11/ref/migration-operations/#django.db.migrations.operations.RunSQL>
tells me that runSQL allows arbitrary SQL to run on the database ... except
for the postgres backend: "On most database backends (all but PostgreSQL),
Django will split the SQL into individual statements prior to executing
them."

  I want to insert data into the two columns of the database. The model
reads:

class Cities (models.Model):
    city_name=models.CharField(max_length=24, unique=True)
    county_name=models.CharField(max_length=12, unique=True)

and I have a list of 419 pairs of names I want to pre-load into this
database table. The rows contain the two variables; e.g.,
  Bend,Deschutes
  Birkenfeld,Columbia
  Blachly,Lane
  Black Butte Ranch,Deschutes

  So, using a postgres back end can I write,

  migrations.RunSQL("INSERT INTO Cities (city_name,county_name) VALUES
('Bend','Dechutes');")

repeated for each row in the table (easy to do with emacs)? And, can I put
this code in models.py after the classes?

Rich

--
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+unsubscribe@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/CAJ22gMA%3Dir3HGr3_7fjcWi9jxQ%2BaY8Rciies43MfD29D%3DxgVNA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment