10

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

1
  • Do not include solution to question please (post a separate answer instead). Commented 3 hours ago

2 Answers 2

18

The pattern I follow is to start with an initial migration:

> python manage.py makemigrations

This creates the migrations to build your table.

Next, I create an empty migration for the data:

> python manage.py makemigrations --empty core

Then I edit the newly created migration file with the data migration using the RunPython function. The pattern in the docs is a little different compared to the other answer here:

https://docs.djangoproject.com/en/1.11/ref/migration-operations/

def create_types(apps, schema_editor):
    db_alias = schema_editor.connection.alias
    Type = apps.get_model("core", "Type")
    Type.objects.using(db_alias).create(property=value)

class Migration(migrations.Migration):

    dependencies = [
         ('core', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_types),
    ]
Sign up to request clarification or add additional context in comments.

1 Comment

Using this method in Django 3.2.6 I get AttributeError: type object 'PriceList' has no attribute 'objects'. Outdated?
0

Firstly, this can't be the first migration, since you need one to actually create your Type table. You at least need to depend on that.

Secondly, once you have the Type model, you treat it just as you would in normal Django code; instantiate it, set its attributes, save it, whatever.

8 Comments

1. This really is my first migration, I created the models and then run migrate - I had the project created but accidentally deleted migration files, so I run the migrations again and it appears like no migrations run before. Really all of my created apps have migrations folder empty. 2. to the second point, in DOC it is recommend to create type like i did, it is in the link attached.
To the first point you were right, I run the migrations but i called only 'makemigrations' with faith it will migrate all my apps, but it did not. I had to run makemigrations for all of my apps solo, now it is ok, so just the question if it is correct to treat model like in 'normal Django code'. Thanks.
And the answer is yes, which means you actually need to save it.
I meant that if I can create it as normal django object, as the doc says it should be done the other way
Look at the link I attached and scroll to the second example, comment in the method combine_names says: "# We can't import the Person model directly as it may be a newer # version than this migration expects. We use the historical version." So it is not safe I guess, am I right? EDIT: I guess I found the solution so if it will work I'll post it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.