django - Type Error : 'authors' is an invalid key argument for this function -
i dont know why getting type error in piece of django app. below models.py code:
from django.db import models # create models here. class publisher(models.model): name = models.charfield(max_length=30) address = models.charfield(max_length=50) city = models.charfield(max_length=60) state_province = models.charfield(max_length=30) country = models.charfield(max_length=30) website = models.urlfield() def __str__(self): return self.name class author(models.model): first_name = models.charfield(max_length=30) last_name = models.charfield(max_length=40) email = models.emailfield(verbose_name=' e-mail') def __str__(self): return "%s %s" %(self.first_name, self.last_name) class book(models.model): title = models.charfield(max_length=100) authors = models.manytomanyfield(author) publisher = models.foreignkey(publisher) publication_date = models.datefield(blank=true, null=true) def __str__(self): return self.title
i have created both author object , publisher object shell named (mak & mas) respectively.
>>> books.models import publisher, author, book >>> mak = author.objects.create(first_name = "kennedy") >>> mas = publisher.objects.create(name = "dreem inc")
but when try populate book database table, type error:
>>> mybook = book(title="the prince", authors=mak, publisher=mas, publication_date = date(1989, 6, 30)) >>> type error : 'authors' invalid key argument function
i can populate 'publisher' , "author" table.
please on this?
you can't assign manytomany field in model's constructor. can't set m2m field unsaved instance. have save instance first , assign m2m field:
mybook = author.objects.create(title="the prince", publisher=mas, publication_date=date(1989, 6, 30)) mybook.authors.add(mak) # or mybook.authors = [mak]
Comments
Post a Comment