Suppose I have a relation as down below:
class Student(AbstractBaseUser):
# It inherits AbstractBaseUser and has it's own manger
# note: I did forget to add editable here
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
firstname
lastname
...
class Teacher(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
firstname
lastname
email
...
class Meeting(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)
student = models.ManyToManyField(Student)
teacher = models.ManyToManyField(Teacher)
How should I add/set a value to field 'student' or 'teacher'. I tried to assign an object from related models to them but got an error:
Direct assignment to the forward side of a many-to-many set is prohibited. Use student.set() instead.
And I tried this as well:
m = Meeting()
s = Student.objects.first() #to get a random student
m.student.add(s)
Then I get: ValueError: Cannot add "<Student: sarah@gmail.com>": instance is on database "None", value is on database "default"
I did this as well:
m = Meeting.objects.create() #It does only have two mentioned fields.
m.student.add(s)
I get this: django.db.utils.ProgrammingError: column "meeting_id" is of type bigint but expression is of type uuid LINE 1: ..._meeting" ("meeting_id", "student_id") VALUES ('c7688df9-... ^ HINT: You will need to rewrite or cast the expression.