在每个makemigrations更改的房地产领域中,请勿更改该领域。是什么原因?!
class Announcement(OJModel):
owner = models.ForeignKey('user.User', verbose_name=_('Announcement Owner'), on_delete=models.CASCADE,
related_name='owner_announcements', null=True, blank=True)
title = models.CharField(max_length=300, default='', verbose_name=_('Title'))
description = models.TextField(max_length=3000, default='', verbose_name=_('Description'))
estate = models.OneToOneField('announcement.Estate', on_delete=models.CASCADE, verbose_name=_('Estate'), null=True)
已生成迁移脚本:
class Migration(migrations.Migration):
dependencies = [
('announcement', '0072_auto_20181218_1256'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='estate',
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, to='announcement.Estate', verbose_name='Estate'),
),
]
答案 0 :(得分:1)
在models.py中这样写:
from django.db import models
class Announcement(models.Model):
owner = models.ForeignKey('user.User', verbose_name=_('Announcement Owner'), on_delete=models.CASCADE,related_name='owner_announcements', null=True, blank=True)
title = models.CharField(max_length=300, default='', verbose_name=_('Title'))
description = models.TextField(max_length=3000, default='',verbose_name=_('Description'))
estate = models.OneToOneField('announcement.Estate', on_delete=models.CASCADE,verbose_name=_('Estate'), null=True)
def __str__(self):
return self.title
在 admin.py 中这样写:
from django.contrib import admin
from .models import Announcement
# Register your models here.
admin.site.register(Announcement)
保存文件,现在从迁移文件夹中删除旧的迁移!
现在在 manage.py 目录的命令行中运行这些:
python manage.py makemigrations
那么:
python manage.py migrate
大功告成!