简体   繁体   中英

How to add value to a many-to-many field in Django

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.

You create a Meeting and than populate it with Teacher s and Student s, so:

s1 = Student.objects.create(firstname='Foo', last_name='Bar')
s2 = Student.objects.create(firstname='Qux', last_name='Qux')
t1 = Teacher.objects.create(
    firstname='Some',
    last_name='One',
    email='someone@something.com'
)

m = Meeting.objects.create()
m.student
m.teacher

For more information, see the Many-to-many relationships section of the documentation .

Based on the exception you shared in the question, you created the Meeting model before you made primary keys of type UUIDField . We can fix this problem by:

  1. removing the Meeting model;
  2. run makemigrations ;
  3. create a new Meeting model;
  4. run makemigrations again; and
  5. run migrate to update the database.

First save Student objects and Teacher objects

>> student_data = {'firstname': 'John', 'last_name', 'Doe', }
>> student1 = Student.objects.create(**student_data )
>>student_data = {'firstname': 'Test', 'last_name', 'Test', }
>> student2 = Student.objects.create(**student_data )

>> teacher_data  = {'firstname': 'TeacherJohn', 'last_name': 'TeacherDoe', 'email': 'john.doe@mail.com'}
>> teacher = Teacher.objects.create(**teacher_data )

Then create a Meeting object with the other fields( other than m2m). Then add/set the m2m objects.

meeting = Meeting.objects.create(**the_model_fields_as_dict)
meeting.student.add(student1, student2)
meeting.teacher.add(teacher )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM