2

This issue is similar to these a and b ,but little different. I am new to django,so please bear with me. Below are my models

class Project(UniqueIdentifier):
    status = models.CharField(max_length=2)

class User(UniqueIdentifier):
    first_name = models.CharField(max_length=50,blank=False,null=False)
    last_name = models.CharField(max_length=50,blank=False,null=False)
    email_address = models.EmailField(max_length=200)
    user_state = models.BooleanField()

    class Meta:
        abstract = True

class Employee(User):
    experience = models.DecimalField(max_digits=4, decimal_places=2)
    userType = models.CharField(max_length=2,choices=USER_TYPES)

class Team(UniqueIdentifier):
    role = models.CharField(max_length=2)
    members = models.ManyToManyField(Employee, through="TeamMember")
    belongsTo = models.ForeignKey(Project, related_name="team")

class TeamMember(UniqueIdentifier):
    team = models.ForeignKey(Team)
    members = models.ForeignKey(Employee)

and in my generic view class

class ProjectView(UpdateView):
    model = Project
    fset = inlineformset_factory(Project,Team,form=TeamForm,can_delete=False,fk_name="belongsTo",extra=0)
    def get_object(self, *args, **kwargs):
        c = get_object_or_404(Project, unique_id = self.kwargs.get('cid'))
        df = ProjectForm(instance=c)
        .......
        return c

    def get_context_data(self, *args, **kwargs):
        context = super(ProjectUpdateView, self).get_context_data(*args, **kwargs) 
        ..........
        return context

    def form_valid(self,form,*args, **kwargs):
        c = get_object_or_404(Project, unique_id = self.kwargs.get('cid'))
        if self.request.method == "POST":
            df = ProjectForm(self.request.POST, instance = c)
            if df.is_valid():
                c = df.save()
                fs = self.fset(self.request.POST,instance=c)
                if fs.is_valid():
                    k=fs.save()

and i am getting this error.

Cannot set values on a ManyToManyField which specifies an intermediary model.

Please suggest if someone has alternative solution (i really don't want to spilt the logic into separate form). Appreciate your help, thanks!

** Updated: Found soluton here. Here is my solution,

Class ProjectView(CreateView):
    .........
    def form_valid():
        .........
        if fs.is_valid():
                    k=fs.save(commit=False)
                    for h in k:
                        h.save()
                    for x in range(0,len(k)):
                        for y in self.request.POST.getlist('team-'+str(x)+'-members'):
                            p=TeamMember(team=k[x],members=get_object_or_404(Employee,unique_id=y))
                            p.save()

Thanks a bunch!

Community
  • 1
  • 1
Satish
  • 141
  • 6

1 Answers1

2

It would be helpful if you had included a stacktrace (instead of just the error). But from the looks of it, you need to save instances of the intermediate model:

Quoting from the docs:

Now that you have set up your ManyToManyField to use your intermediary model, you're ready to start creating some many-to-many relationships. You do this by creating instances of the intermediate model ... Unlike normal many-to-many fields, you can't use add, create, or assignment to create relationships ... The only way to create this type of relationship is to create instances of the intermediate model.

Roshan Mathews
  • 5,788
  • 2
  • 26
  • 36