I'm build models in Django 1.8, and I'm using abstract inheritance (which I'm assuming is contributing to the problem). I have abstract models and then I have models which are based on those abstract models. I also have ForeignKey and ManyToMany relations between some models.
Everything looks fine, but when I try to syncdb or 'makemigrations blog' I get an AttributeError which says 'NoneType' object has no attribute 'unique'.
I don't know why I'm getting it, and I tried experimenting with different model setups, and I read lots of forum posts, but for now I've hit a wall.
I'll post the traceback and my models below:
MODELS:
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)
blog:
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'
IF this is caused by my inherited models, what is the exact cause, and how should I change things?
Thanks in advance.
The Python "AttributeError: 'NoneType' object has no attribute 'get'" occurs when we try to call the get() method on a None value, e.g. assignment from function that doesn't return anything. To solve the error, make sure to only call get() on dict objects.
The Python "AttributeError: 'NoneType' object has no attribute 'shape'" occurs when we access the shape attribute on a None value, e.g. after passing an incorrect path to cv2. imread() . To solve the error, make sure to specify the correct path.
When ever you get a problems that involves a message such as " 'nonetype' object has no attribute ..." it means the same thing: you have tried to call a method on something that doesn't exist. If you try to do anything with that value, you will get this error.
NoneType in Python is a data type that simply shows that an object has no value/has a value of None . You can assign the value of None to a variable but there are also methods that return None .
I believe the issue is actually with the Work
models.
You have a ForeignKey to URL
in the abstract class,
link = models.ForeignKey(URL)
And you also have keys to URL
in some of the derived classes, for example MusicalWork:
class MusicalWork(Work):
audio_links = models.ManyToManyField(URL)
So MusicalWork has two links to URL. Which would be fine, except Django attempts to create a reverse relationship for URL to your model, usually called musicalwork_set
for this case, but it has two reverse relationships for the same model!
The answer would be to specify a related_name
field for any models that derive from it with the same model references.
Therefore:
class MusicalWork(Work):
audio_links = models.ManyToManyField(URL, related_name='musicalwork_audio_set')
But there may be other issues as that error message doesn't exactly point to this condition (trust me, Django has a much nicer error message for this particular mistake).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With