0

Hi I am new learner of Django and would like to figure out how to load some data into the database using a file(.py).

There are three tables in the database.

class region(models.Model):
    region_name = models.CharField(primary_key=True,max_length=50, null=False)


class country(models.Model):
    country_name = models.CharField(primary_key=True,max_length=40, null=False)
    region = models.ForeignKey(region)

class office(models.Model):
    office_name = models.CharField(primary_key=True,max_length=50, null=False)
    address = models.CharField(max_length=200, null=False)
    postal_code = models.IntegerField(null = False)
    country = models.ForeignKey(country)

Initially I use the python shell of enter some simple data into the db using

>>> r = region(region_name='asia')
>>> r.save()

Can anyone help to create such a file to load data of region, country, office into the database? as there are sets of region,country,office that really need to be there.

Thank you very much for helping out!

2
  • Which database are you using? Generally the bulk loading tools that come with the database are a better way to load data in than writing your own. Commented May 29, 2013 at 3:58
  • @hd1 postgres, yea now I see should use the fixture Commented May 29, 2013 at 4:13

1 Answer 1

1

You can provide initial data to your models by using fixtures. For that, you can store all data in a file named initial_data.xml (or yaml/json] file in the fixtures directory of your app. You can see the django reference for details

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.