1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How do I create new objects in django data migration?

Discussão em 'Python' iniciado por Stack, Novembro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have a new django project with no migrations before, therefore I want to create my first migration which inserts some rows into a table. This is what I have yet:

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals

    from django.db import migrations, models


    def create_types(apps, schema_editor):
    Type = apps.get_model("core", "Type")
    type = Type()


    class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
    migrations.RunPython(create_types)
    ]


    I have an application core which has model named Type, and I want to create few types and set attributes for them, and all of them save into database. How to create a new object of Type model here? Also other question, if this is very first migrator can I leave the dependencies empty? The file I created with this tutorial https://docs.djangoproject.com/en/1.8/topics/migrations/#data-migrations named it 0001_initial.py

    Continue reading...

Compartilhe esta Página