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()
Related
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
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 problem is driving me nuts - I can't spot what's going on so I was hoping that someone would be able to point out what I'm missing here.
I've got two sample models, FootballClub, FootballPitch, as defined below.
class FootballClub(models.Model)
name = models.CharField(max_length=100)
class Meta:
ordering = ['name']
def __unicode__(self):
return self.name
class FootballPitch(models.Model):
name = models.CharField(max_length=100)
owning_club = models.ForeignKey(FootballClub)
I've made a modification to the FootballPitch class, more specifically the owning_club field, to add null=True to it, so the class now looks like this;
class FootballPitch(models.Model):
name = models.CharField(max_length=100)
owning_club = models.ForeignKey(FootballClub, null=True)
(I don't need to manage this model through a form so I don't care that blank=True is not set).
I have also run makemigrations and migrate on this model.
When trying to list out all instances of FootballPitch that have owning_club set to null (using FootballPitch.objects.filter(owning_club__isnull=True)) I get the following error;
Programming Error: relation "footballclub_footballpitch" does not exist
LINE 1: ...d", "footballclub_footballpitch"."name",FROM "footballclub_f...
Anyone have any ideas what is going wrong? (Django 1.8.18 and Python 2.7 and postgres 9.8 for reference)
Thanks in advance!
Edit: Due to request added migrations code here instead of in reply to comment requesting it;
./manage.py --list outputs the following;
[X] 0001_initial [X] 0002_auto_20191009_1409
The migration contains # -- coding: utf-8 -- from future import unicode_literals from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('football', '0001_initial'), ] operations = [ migrations.AlterField( model_name='footballpitch', name='owning_club', field=models.ForeignKey(to='football.FootballClub', null=True), ), ]
Edit++ to include initial migration
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
import django_extensions.db.fields
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='FootballClub',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=100)),
],
options={
'ordering': ['name'],
},
),
migrations.CreateModel(
name='FootballPitch',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=100)),
('owning_club', models.ForeignKey(to='football.FootballClub')),
],
),
]```
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.
I'm pretty new at Django 2.1 framework.
and I am a week trying to setup the tables for my app. Settings are fine I listed my app in INSTALLED_APPS, but when I try to run manage.py migrate code it gives me one auto_table instead of the ones that was written on model file.
These are my models.
Models.py
from django.db import models
class Nome (models.Model):
titulo = models.CharField(max_length=100),
objetivo = models.CharField(max_length=100),
class Sobrenome (models.Model):
lets = models.ForeignKey(Nome, on_delete=models.CASCADE),
make = models.CharField(max_length=100),
That's what migrate code gave to me:
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Dreams',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Wish',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
The problem is that you have ended each line in your models definitions with a comma. This makes each attribute a tuple, so it is not recognized as an actual field. Remove the commas:
class Nome (models.Model):
titulo = models.CharField(max_length=100)
objetivo = models.CharField(max_length=100)
and run makemigrations again.