Hello all! This is my first time working with Django and I was curious if anyone could point me in the right direction. I have a Django app that I am doing some work in. I have three separate models with different attributes in them. Below is what the code looks like:
from django.db import models
class Universe(models.Model):
"""A fictional universe."""
name = models.CharField(max_length=100)
slug = models.SlugField()
def __str__(self):
return self.name
class Show(models.Model):
"""A television show within a universe."""
name = models.CharField(max_length=100)
universe = models.ForeignKey(Universe, on_delete=models.CASCADE)
slug = models.SlugField()
theme = models.CharField(max_length=100, blank=True)
seasons = models.IntegerField(blank=True, null=True)
episodes = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.name
class Character(models.Model):
"""A character on a show."""
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
actor = models.CharField(max_length=200)
shows = models.ManyToManyField(Show)
slug = models.SlugField()
objects = CharacterManager()
def __str__(self):
return '{self.first_name} {self.last_name}'.format(self=self)
class Meta(object):
ordering = ('slug', )
The goal is to make it so that a user can create a character, show, and universe in one call using a Character manager.
By digging through the docs I came to this block of code:
class CharacterManager(models.Manager):
def create_with_show_and_universe(self, first_name, last_name, actor,
slug, show_name, show_slug,
universe_name, universe_slug):
first_name = self.create(first_name=first_name)
last_name = self.create(last_name=last_name)
actor = self.create(actor=actor)
slug = self.create(slug=slug)
Show.name = show_name
Show.slug = show_slug
Universe.name = universe_name
Universe.slug = universe_slug
The issue is that I am getting the below error when I try to run the test:
AttributeError: 'NoneType' object has no attribute 'first_name'
This is the test I run:
def test_create_new_character_show_and_universe(self):
character = models.Character.objects.create_with_show_and_universe(
first_name='Blanche',
last_name='Devereaux',
actor='Rue McClanahan',
slug='blanche_devereaux',
show_name='Golden Girls',
show_slug='golden_girls',
universe_name='Golden Girls',
universe_slug='golden_girls'
)
self.assertEqual(character.first_name, 'Blanche')
self.assertEqual(character.last_name, 'Devereaux')
self.assertEqual(character.actor, 'Rue McClanahan')
self.assertEqual(character.slug, 'blanche_devereaux')
I am still digging through the docs surrounding models and model managers. Any help would be appreciated.