I use Django framework
This is my models.py
from django.db import models
# Create your models here.
class Destination(models.Model):
name: models.CharField(max_length=100)
img: models.ImageField(upload_to='pics')
desc: models.TextField
price: models.IntegerField
offer: models.BooleanField(default=False)
and here is my migrations folder-0001_initial.py:
# Generated by Django 4.1.3 on 2022-11-17 10:17
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Destination',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
it don't migrate my model, i deleted 0001_initial.py and pycache and migrate again but it worked the same
How can i make migrations with my models ?
You have added model fields in incorrect way. You can't add like this.
change this:
class Destination(models.Model):
name: models.CharField(max_length=100)
img: models.ImageField(upload_to='pics')
desc: models.TextField
price: models.IntegerField
offer: models.BooleanField(default=False)
To this:
class Destination(models.Model):
name = models.CharField(max_length=100)
img = models.ImageField(upload_to='pics')
desc = models.TextField()
price = models.IntegerField()
offer = models.BooleanField(default=False)
After changing above code. Try to migrate manually using below commands:
python manage.py makemigrations appname
python manage.py sqlmigrate appname 0001 #This value will generate after makemigrations. it can be 0001 or 0002 or more
python manage.py migrate
Related
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')),
],
),
]
First a basic explanation and below an answer to your specific situation
There is 2 steps:
"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.
"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.
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).
from django.db import models
class Town(models.Model):
name: models.CharField(max_length=70,unique=True)
country: models.CharField(max_length=30,unique=True)
class Meta:
pass
This is my model Town whith two attributes: name and country. When I create a migration in the initial_0001.py file only id column is shown
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Town',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
What kind of problem could it be?
The ID-Field is always automatically generated by Django when making migrations. You can specifiy your own ID field aswell, but using an auto-incremented like this is fine for your use case.
You also might want to get rid of the unique=True, as it would prevent adding multiple towns from the same country.
Create your model like this and redo your migration process:
class Town(models.Model):
name = models.CharField(max_length=70)
country = models.CharField(max_length=30)
When you have 'no changes detected' when making migrations, I usually follow these steps:
First, Delete migrations folder in project.
Then delete django-migration entries in your Database with this query:
DELETE from django_migrations WHERE app='yourAppName'
Then, create new folder ‘migrations” in app folder + init.py
Lastly, redo the migration process:
py manage.py makemigrations
py manage.py migrate --run-syncdb
I had the same problem, and this solved it. Hope it helps.
Instead of : it should be =
You also need to provide a default value for all the previous rows that were created
Then run python manage.py makemigrations
Run python manage.py migrate
class Town(models.Model):
name = models.CharField(max_length=70,unique=True, default='')
country = models.CharField(max_length=30,unique=True, default='')
class Meta:
pass
Unique Class or extend Class or Subclass in Python Django?
In the following situation, I have a feeling I need to ?extend? the Migration class instead of re-stating it in the second module. Or is a child class needed?
A goal here: To create a postgres table called venues. There is already a models/venues.py that seems to be set up ok.
migrations/0001_initial.py:
class Migration(migrations.Migration):
initial = True
dependencies = [('auth', '0012_alter_user_first_name_max_length'),]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, ...)),
('password', models.CharField(max_length=128, ...)),
...
migrations/0002_venue.py:
class Migration(migrations.Migration):
dependencies = [('app', '0001_initial'),]
operations = [
migrations.CreateModel(
name='Venue',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True...)),
('name', models.CharField(blank=True ...)),
('address', models.CharField(blank=True...)),
...
** models/venue.py:**
class Venue(models.Model):
name = models.CharField(blank=True, null=True...)
address = models.CharField(blank=True, null=True...)
city = models.CharField(blank=True, null=True, ...)
zip = models.CharField(blank=True, null=True...)
#gps_coords = models.CharField(blank=True...)
gps_lat = models.DecimalField(max_digits=14...)
gps_long = models.DecimalField(max_digits=14...)
description = models.TextField(blank=True, ...)
website = models.URLField(blank=True...)
contact_phone = models.CharField(blank=True...)
contact_email = models.EmailField(blank=True...)
contact_name = models.CharField(blank=True...)
def __str__(self):
return self.name + " " + self.description
Help?
Once you create your model class, you need to run python manage.py makemigrations and django will create a migration file. (Make sure you've added the app to the INSTALLED_APPS on project's settings.py
Once you run the makemigrations, you'll be able to see the migration file in your app's migrations folder. Having this file doesn't mean the table is created. It just represents the set of instructions that'll run on database when you run the migrate command.
When you have the new migration file, you can run python manage.py migrate, and that migration file will be applied to your database.
You can run the showmigrations command to see which migrations are applied on your database.
python manage.py showmigrations
or
python manage.py showmigrations app_name
This is an easy exercise, i'm a beginner about models and migration
Models.py
from django.db import models
# Create your models here.
class Flight(models.Model):
origin = models.CharField(max_length=64),
destination = models.CharField(max_length=64),
duration = models.IntegerField()
Then i'm going on my prompt and type
python manage.py makemigrations
and in migrations/0001_initial.py
# Generated by Django 3.1.7 on 2021-03-23 16:19
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Flight',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('duration', models.IntegerField()),
],
),
]
How u can see origin and destination don't migrate
How can i fix it?
remove ',' now try makemigrations
class Flight(models.Model):
origin = models.CharField(max_length=64)
destination = models.CharField(max_length=64)
duration = models.IntegerField()
I'm looking to use the PostgreSQL-specific ArrayField in my Django project, but it doesn't show up in the migrations after I run makemigrations. Any ideas why?
Django v2.1
Postgresql v9.6.6
# Models.py
from django.db import models
from django.contrib.postgres.fields import ArrayField
class MyClassName(models.Model):
udi = ArrayField(models.CharField()),
version = models.IntegerField()
Then I run: python3 manage.py makemigrations
# 0001_initial.py
migrations.CreateModel(
name='MyClassName',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('version', models.IntegerField()),
],
),
As you can see, the field 'udi' is suspiciously missing.
The problems are a comma at the end of the ArrayField() and CharField would need max_length too.
class MyClassName(models.Model):
udi = ArrayField(models.CharField(max_length=10))
version = models.IntegerField()
Run makemigrations again and you will get a migration you want.