我在Django 1.8中构建模型,我正在使用抽象继承(我假设这是导致问题的原因)。我有抽象模型,然后我有基于那些抽象模型的模型。我也有一些模型之间的ForeignKey和ManyToMany关系。
一切看起来都不错,但是当我尝试syncdb或'makemigrations blog'时,我得到一个AttributeError,其中说'NoneType'对象没有属性'unique'。
我不知道为什么我会得到它,我尝试了不同的模型设置,并且我阅读了很多论坛帖子,但是现在我已经碰壁了。
我将在下面发布追溯和我的模型:
模型:
indie_db
from django.db import models
class URL(models.Model):
link = models.CharField(max_length=400)
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Artist(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
link = models.ForeignKey(URL)
class Meta:
abstract = True
ordering = ['name']
def __str__(self):
return self.name
class ArtistSingle(Artist):
birthdate = models.DateField(null=True, blank=True)
deathdate = models.DateField(null=True, blank=True)
class ArtistGroup(Artist):
members = models.ManyToManyField(ArtistSingle)
established = models.DateField(null=True, blank=True)
disbanded = models.DateField(null=True, blank=True)
class Contributor(models.Model):
contributing_artist = models.ForeignKey(ArtistSingle, null=True, blank=True)
alternate_name = models.CharField(max_length=200)
role = models.CharField(max_length=200)
class ProductionCompany(models.Model):
name = models.CharField(max_length=200)
link = models.ForeignKey(URL)
def __str__(self):
return self.name
class Work(models.Model):
title = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
created = models.DateField()
city = models.CharField(max_length=200)
production_company = models.ForeignKey(ProductionCompany, blank=True, null=True)
self_published = models.BooleanField(default=False)
creator = models.ForeignKey(Artist)
link = models.ForeignKey(URL)
styles = models.CharField(max_length=200)
contributors = models.ManyToManyField(Contributor)
class Meta:
abstract = True
ordering = ['-created']
def __str__(self):
return self.title
class MusicalWork(Work):
audio_links = models.ManyToManyField(URL)
class WrittenWork(Work):
excerpt = models.TextField(null=True, blank=True)
class PerformanceWork(Work):
venue = models.CharField(max_length=200)
class VideoWork(Work):
length = models.CharField(max_length=16)
class VisualWork(Work):
images = models.ManyToManyField(URL)
博客:
from django.db import models
# Create your models here.
class Tag(models.Model):
tag_name = models.CharField(max_length=200)
def __str__(self):
return self.tag_name
class Entry(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
slug = models.SlugField(max_length=200)
publish = models.BooleanField(default=True)
created = models.DateTimeField(auto_now=True)
modified = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField(Tag)
def __str__(self):
return self.title
class Meta:
verbose_name = "Blog Entry"
verbose_name_plural = "Blog Entries"
ordering = ["-created"]
TRACEBACK:
[pattmayne@web476 weird_canada]$ python3.4 manage.py makemigrations blog
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/base.py", line 440, in execute
self.check()
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/base.py", line 478, in check
include_deployment_checks=include_deployment_checks,
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/checks/model_checks.py", line 28, in check_all_models
errors.extend(model.check(**kwargs))
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/base.py", line 1181, in check
errors.extend(cls._check_fields(**kwargs))
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/base.py", line 1258, in _check_fields
errors.extend(field.check(**kwargs))
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/fields/related.py", line 1829, in check
errors = super(ForeignKey, self).check(**kwargs)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/fields/related.py", line 1502, in check
errors.extend(self._check_unique_target())
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/fields/related.py", line 1522, in _check_unique_target
for rel_field in self.foreign_related_fields)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/fields/related.py", line 1522, in <genexpr>
for rel_field in self.foreign_related_fields)
AttributeError: 'NoneType' object has no attribute 'unique'
如果这是由我继承的模型引起的,究竟是什么原因,我应该如何改变呢?
提前致谢。
答案 0 :(得分:3)
我认为问题实际上是Work
模型。
在抽象类中有一个ForeignKey URL
,
link = models.ForeignKey(URL)
在某些派生类中你也有URL
的键,例如MusicalWork:
class MusicalWork(Work):
audio_links = models.ManyToManyField(URL)
所以MusicalWork有两个指向URL的链接。除了Django尝试为您的模型创建URL的反向关系(通常在这种情况下称为musicalwork_set
),但是对于同一模型它有两个反向关系,这样会没问题。
答案是为使用相同模型引用从中派生的任何模型指定related_name
字段。
因此:
class MusicalWork(Work):
audio_links = models.ManyToManyField(URL, related_name='musicalwork_audio_set')
但是可能还有其他问题,因为错误消息并没有完全指向这种情况(相信我,Django对于这个特定的错误有更好的错误信息)。
答案 1 :(得分:2)
这是因为您有一个抽象模型的外键。
creator = models.ForeignKey(Artist)
您可以将其更改为ArtistSingle
creator = models.ForeignKey(ArtistSingle)
理想情况下它应该抛出正确的错误,如
字段定义与模型“艺术家”的关系,该模型要么未安装,要么是抽象的。