Tuesday, 2 April 2019

django 26 migration add field

added date posted field to album
date posted field on existing albums is auto filled with migration time

0004 migration applied

add author field to album, chuanshuo is the default author

migration 005 applied

#music/models

from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.contrib.auth.models import User

class Album(models.Model):
    artist = models.CharField(max_length=50)
    album_title = models.CharField(max_length=50)
    genre = models.CharField(max_length=50)
    album_logo = models.FileField()
    date_posted = models.DateTimeField(default=timezone.now())
    author = models.ForeignKey(User, on_delete=models.CASCADE, default=1)

    #form submitted without action redirect to detail
    def get_absolute_url(self):
        return reverse('music:detail', kwargs={'pk': self.pk})

    #query album.objects.get(pk=1)
    def __str__(self):
        return self.album_title + ' - ' + self.artist

class Song(models.Model):
    album = models.ForeignKey(Album, on_delete=models.CASCADE)
    file_type = models.CharField(max_length=50)
    song_title = models.CharField(max_length=50)
    is_favorite = models.BooleanField(default=False)

    def __str__(self):
        return self.song_title

----------------------------------
#power shell

PS C:\Users\bob\django\project1> python manage.py makemigrations
System check identified some issues:

WARNINGS:
music.Album.date_posted: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Migrations for 'music':
  music\migrations\0004_album_date_posted.py
    - Add field date_posted to album
PS C:\Users\bob\django\project1> python manage.py migrate
System check identified some issues:

WARNINGS:
music.Album.date_posted: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, music, sessions
Running migrations:
  Applying music.0004_album_date_posted… OK
PS C:\Users\bob\django\project1>

-----------------------------------------
#music/migrations/0004

# Generated by Django 2.2b1 on 2019-04-03 03:24

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

    dependencies = [
        ('music', '0003_auto_20190319_0700'),
    ]

    operations = [
        migrations.AddField(
            model_name='album',
            name='date_posted',
            field=models.DateTimeField(default=datetime.datetime(2019, 4, 3, 3, 24, 26, 446861, tzinfo=utc)),
        ),
    ]
-----------------------------------------
#music/migrations/0005

# Generated by Django 2.2b1 on 2019-04-03 04:22

import datetime
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
from django.utils.timezone import utc


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('music', '0004_album_date_posted'),
    ]

    operations = [
        migrations.AddField(
            model_name='album',
            name='author',
            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
        ),
        #date_posted is fixed after creation, won't be altered by migration
        migrations.AlterField(
            model_name='album',
            name='date_posted',
            field=models.DateTimeField(default=datetime.datetime(2019, 4, 3, 4, 22, 48, 248016, tzinfo=utc)),
        ),
    ]

No comments:

Post a Comment