Home > OS >  Django: 0001_initial.py is not on current status after complementing models.py
Django: 0001_initial.py is not on current status after complementing models.py

Time:01-12

I am new to Django/python and I am facing a problem with my models.py. I added some attributes, saved it -> py manage.py makemigrations -> py manage.py migrate but the current attributes are not shown in the 0001_initial.py. Also when I am opening the database in my DB Browser for SQLite I still get the old status.

Here's my code:

models.py

from django.db import models


# from django.contrib.auth.models import User

# Create your models here.
    category_choice = (
        ('Allgemein', 'Allgemein'),
        ('Erkältung', 'Erkältung'),
        ('Salben & Verbände', 'Salben & Verbände'),
    )

class medicament(models.Model):
    PZN = models.CharField(max_length=5, primary_key=True) # Maxlaenge auf 5 aendern
    name = models.CharField('Medikament Name', max_length=100)
    description = models.CharField('Medikament Beschreibung', max_length=500)
    category = models.CharField(max_length=100, blank=True, null=True, choices=category_choice)
    instructionsForUse = models.CharField('Medikament Einnehmhinweise', max_length=400)
    productimage = models.ImageField(null=True, blank=True, upload_to="images/")
    stock = models.PositiveIntegerField(default='0')
    reorder_level = models.IntegerField(default='0', blank=True, null=True)
    price= models.DecimalField(default='0.0', max_digits=10, decimal_places=2)
    sold_amount = models.IntegerField(default='0', blank=True, null=True)
    sales_volume =  models.DecimalField(default='0.0', max_digits=10, decimal_places=2)


    def __str__(self):
        return self.name

And the 0001_initial.py

# Generated by Django 3.2.16 on 2023-01-05 14:33

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='medicament',
            fields=[
                ('PZN', models.CharField(max_length=5, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=100, verbose_name='Medikament Name')),
                ('description', models.CharField(max_length=500, verbose_name='Medikament   Beschreibung')),
                ('category', models.CharField(default='Allgemein', max_length=100)),
                ('instructionsForUse', models.CharField(max_length=400, verbose_name='Medikament Einnehmhinweise')),
                ('productimage', models.ImageField(blank=True, null=True, upload_to='images/')),
                ('stock', models.IntegerField(default='0')),
            ],
        ),
    ]

CodePudding user response:

First a basic explanation and below an answer to your specific situation

There is 2 steps:

  1. "makemigrations" scans your code, finds changes in models an will create migrations files according to changes like
001_initial.py
002_auto_xyz.....py

There is an initial file and then subsequent migration files having a running number and depend on each other which you can see in the file e.g.

    dependencies = [
        ('users', '0003_auto_20210223_1655'),
    ]

Once a migration file is created it will not change anymore and all later modifications in the code will be reflected in new additional migrations file after a new makemigration is executed.

  1. "migrate" will take the created migration files, will check your database which migrations have been applied already to this specific database(!) and apply the once that are pending. In the database there is a table created that stores info about the already applied migrations.

That means for example you can run a migrate on a database on your development server and independent from that run migrate on a database on your production server. Both migrate commands will read the existing migration files and perform migrations in the database.

So if you made an initial model -> makemigrations -> migrate, you will see an 001_initial.py migrations file.

If you make changes to the models -> makemigrations again -> migrate again you should find only the changes in the 002_... migrations file and find also the changes in your database.

CodePudding user response:

In any Django project, you only run makemigrations once (at the first initialization of your models), after that you run ONLY migrate for any updates.

As for your problems, you should delete your SQLite DB, and also delete all migrations files that end with .pyc.

After that run makemigrations then migrate, (and don't run makemigrations again, the reason being that it will cause problems and collisions with older SQL migrations based on the previous makemigrations).

  • Related