I have an issue here. I have my form with one field (many to many), this field maps to a m2m table with the atributes id_academic_degree id_employee and license. When i try to save it, django displays an error like this
Cannot set values on a ManyToManyField which specifies an intermediary model. Use employees.EmployeeAcademicDegree's Manager instead.
View
CreateEmployee(CreateView):
model = Employee
form_class = EmployeeForm
template_name = 'CreateEmployee.html'
@transaction.atomic
def form_valid(self, form):
form.save()
form.save_m2m()
messages.success(self.request, 'El Empleado fue guardado Exitosamente.')
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
return reverse('physicians:CreatePerson',)
my model
class Employee(models.Model):
person = models.OneToOneField(Person)
region = models.ForeignKey(Region, db_column='id_region')
academic_degrees = models.ManyToManyField(AcademicDegree,
through='EmployeeAcademicDegree', blank=True)
active = models.NullBooleanField()
rfc = models.CharField(max_length=15, blank=True, null=True)
shift_start = models.TimeField(blank=True, null=True)
shift_end = models.TimeField(blank=True, null=True)
history = HistoricalRecords() # Always leave at the end.
class Meta:
db_table = 'employee'
def __str__(self):
return self.person.full_name
def __unicode__(self):
return self.person.full_name
class EmployeeAcademicDegree(models.Model):
employee = models.ForeignKey(Employee, db_column='id_employee')
academic_degree = models.ForeignKey(AcademicDegree,
db_column='id_academic_degree')
license = models.CharField(max_length=15, blank=True, null=True)
history = HistoricalRecords() # Always leave at the end.
class Meta:
db_table = 'employee_academic_degree'
def __str__(self):
return unicode(self.academic_degree)
def __unicode__(self):
return unicode(self.academic_degree)
And the tables are the following
Employee
id
id_person
id_region
active(boolean)
rfc
shift_start
shift_end
employee_academic_degrees
id_employee_academic_degrees
id_employee(FK)
id_academic_degree(FK)
license
I suppose that i need to save first the relation then the whole object (but i dont know how to save the license, because the form doesnt display any field to fill the license)