ich beschäftige mich derzeit mit der many-to-many-Beziehung in Django. Im Admin-Panel klappt die Eingabe von Daten, nun möchte ich das auch mit der python-shell machen. Hier habe ich aber Probleme. Mein Model sieht wie folgt aus:
Code: Alles auswählen
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=255)
authors = models.ManyToManyField("Author", related_name="books")
def __str__(self):
return self.title
class Author(models.Model):
name = models.CharField(max_length=255, unique=True)
def __str__(self):
return self.name
Code: Alles auswählen
from books.models import Author, Book
book1 = Book(title='Python unchained', authors='Guido van Rossum')
Code: Alles auswählen
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use authors.set() instead
LG
Kerstin