Django inserts default data after migration

I want my application to have default data, such as user types. The most efficient way to manage default data after migration.

he needs to handle situations such as, after adding a new table, he adds default data to it.

+11
source share
4 answers

Update:

most users are looking for data migration, as suggested by @durdenk at fooobar.com/questions/1257370 / .... But the OP was asking about a way to add data after migration , so this is an accepted answer.

Original answer :

I think what you are looking for is fixtures https://docs.djangoproject.com/en/1.10/howto/initial-data/

From the documents

Sometimes it’s useful to pre-populate the database with hard-coded data when you first set up the application. You can provide baseline data using instruments.

Also read this https://code.djangoproject.com/wiki/Fixtures

+4
source

You need to create an empty migration file and make your material in the operations block, as described in the documents.

Data migration

Besides changing the database schema, you can also use migrations to modify the data in the database itself in combination with the schema, if you want.

Now all you have to do is create a new function and use RunPython

The docs explain this with an example to show how to communicate with your models.

From Documents

To create an empty migration file,

 python manage.py makemigrations --empty yourappname 

And this is an example of updating an updated field.

 # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import migrations, models def combine_names(apps, schema_editor): # We can't import the Person model directly as it may be a newer # version than this migration expects. We use the historical version. Person = apps.get_model("yourappname", "Person") for person in Person.objects.all(): person.name = "%s %s" % (person.first_name, person.last_name) person.save() class Migration(migrations.Migration): initial = True dependencies = [ ('yourappname', '0001_initial'), ] operations = [ migrations.RunPython(combine_names), ] 
+33
source

The accepted answer is in order. But, since the OP asked the question in the context of adding new lines, not updating existing records. Here is a snippet of code to add new entries:

 from django.db import migrations class Migration(migrations.Migration): dependencies = [ ('loginmodule', '0002_login_avatar'), ] def insertData(apps, schema_editor): Login = apps.get_model('loginmodule', 'Login') user = Login(name = "admin", login_id = "admin", password = "password", email = " admin@pychat.com ", type = "Admin", avatar="admin.jpg") user.save() operations = [ migrations.RunPython(insertData), ] 
+2
source

The answer is given above to show how to insert new rows into the table.

 from django.db import migrations, models from yourapp.models import <yourmodel> def combine_names(apps, schema_editor): obj = <yourmodel>(arrib=value) obj.save() 

For example, let's say you have a Person model

 person = Person(first_name='raj', last_name='shah') person.save() 
-1
source

Source: https://habr.com/ru/post/1257370/


All Articles