这是我的模型和迁移文件。摘自Django-Unleashed-第10章。
Models.py:
class Tag(models.Model):
def get_update_url(self):
return reverse('organizer_tag_update',
kwargs={'slug': self.slug})
def get_absolute_url(self):
return reverse('organizer_tag_detail',kwargs={'slug':self.slug})
def get_delete_url(self):
return reverse('organizer_tag_delete',
kwargs={'slug': self.slug})
name = models.CharField(
max_length=31, unique=True)
slug = models.SlugField(
max_length=31,
unique=True,
help_text='A label for URL config.'
)
class Meta:
ordering=['name']
def __str__(self):
return self.name
class Startup(models.Model):
class Meta:
ordering=['name']
get_latest_by=['founded_date']
name = models.CharField(
max_length=31, db_index=True)
slug = models.SlugField(
max_length=31,
unique=True,
help_text='A label for URL config.')
description = models.TextField()
founded_date = models.DateField('date founded')
contact = models.EmailField()
website = models.URLField(max_length=255)
tags = models.ManyToManyField(Tag,blank=True)
def __str__(self):
return self.name
def get_update_url(self):
return reverse('organizer_startup_update',kwargs={'slug': self.slug})
def get_delete_url(self):
return reverse('organizer_startup_delete',kwargs={'slug': self.slug})
def get_absolute_url(self):
return reverse('organizer_startup_detail',kwargs={'slug': self.slug})
Migrations.py:
#startup_data.py SECOND MIGRATION FILE
from datetime import date
from django.db import migrations, models
STARTUPS = [
{
"name": "Arachnobots",
"slug": "arachnobots",
"contact": "contact@arachnobots.com",
"description":
"Remote-controlled internet-enabled "
"Spider Robots.",
"founded_date": date(2014, 10, 31),
"tags": ["mobile", "augmented-reality"],
"website":
"http://frightenyourroommate.com/",
},
{
"name": "Boundless Software",
"slug": "boundless-software",
"contact": "hello@boundless.com",
"description": "The sky was the limit.",
"founded_date": date(2013, 5, 15),
"tags": ["big-data"],
"website": "http://boundless.com/",
},
]
def add_startup_data(apps, schema_editor):
Startup = apps.get_model(
'organiser', 'Startup')
Tag = apps.get_model('organiser', 'Tag')
for startup in STARTUPS:
startup_object = Startup.objects.create(
name=startup['name'],
slug=startup['slug'],
contact=startup['contact'],
description=startup['description'],
founded_date=startup['founded_date'],
website=startup['website'])
for tag_slug in startup['tags']:
startup_object.tags.add(
Tag.objects.get(
slug=tag_slug))
def remove_startup_data(apps, schema_editor):
Startup = apps.get_model(
'organiser', 'Startup')
for startup in STARTUPS:
startup_object = Startup.objects.get(
slug=startup['slug'])
startup_object.delete()
class Migration(migrations.Migration):
dependencies = [
('organiser', '0002_tag_data'),
]
operations = [
migrations.RunPython(
add_startup_data,
remove_startup_data)
]
#tagdata.py FIRST MIGRATION FILE
TAGS = (
# ( tag name, tag slug ),
("augmented reality", "augmented-reality"),
("big data", "big-data"),
("django", "django"),
("education", "education"),
("ipython", "ipython"),
("javascript", "javascript"),
("jupyter", "jupyter"),
("mobile", "mobile"),
("node.js", "node-js"),
("php", "php"),
("python", "python"),
("ruby on rails", "ruby-on-rails"),
("ruby", "ruby"),
("video games", "video-games"),
("web", "web"),
("zend", "zend"),
)
def add_tag_data(apps,schema_editor):
Tag=apps.get_model('organiser','Tag')
for tag_name,tag_slug in TAGS:
Tag.objects.create(
name=tag_name,
slug=tag_slug
)
def remove_tag_data(apps,schema_editor):
Tag=apps.get_model('organiser','Tag')
for _,tag_slug in TAGS:
tag=Tag.objects.get(slug=tag_slug)
tag.delete()
class Migration(migrations.Migration):
dependencies = [
('organiser', '0001_initial'),
]
operations = [
migrations.RunPython(
add_tag_data,
remove_tag_data )
]
完全错误:
migrations \ 0003_startup_data.py”,第1行13,位于add_startup_data中 startup_object.tags.add(AttributeError:'Startup'对象没有属性'tags'
当我尝试运行migrate
命令时出现错误。
答案 0 :(得分:0)
在0001_initial.py中添加它可以解决它...
operations = [
........ # other code here
migrations.AddField(
model_name='startup',
name='tags',
field=models.ManyToManyField(to='organiser.Tag'),
),
]