Reputation: 375
So I have 2 models: Team and Member. I have a pivot table called MemberTeam.
Here is my MemberTeam model:
member = models.ForeignKey(Member)
team = models.ForeignKey('acpkinballmanageteams.Team',verbose_name=_("team"))
role = models.CharField(max_length=15,choices=ROLE_CHOICES,verbose_name=_("role"),blank=False,default='player')
So when an admin creates a team, I wanted for them to add the players directly. So I created an inlineformset. Here is my MemberTeamForm:
class MemberTeamForm(forms.ModelForm):
member = AutoCompleteSelectField(lookup_class=MemberLookup2,required=True)
class Meta:
model = MemberTeam
Nothing to fancy. And here is how I created the formsets:
TeamRosterFormset = inlineformset_factory(Team, MemberTeam, fields=('member','role'), \
form=MemberTeamForm,can_delete=True, extra=1, max_num=18)
Everything works fine if I create a team and add no members. It also works fine if I add/edit/remove members from the team edit page. The problem is when I try to add a team with a member, I get this error: "" needs to have a value for field "team" before this many-to-many relationship can be used.
Here is the view that is being used (It's a Create-form based view):
def form_valid(self, form):
context = self.get_context_data()
self.object = form.save(commit=False)
teamroster_formset = cp_forms.TeamRosterFormset(self.request.POST, instance=self.object)
if teamroster_formset.is_valid():
teamroster_formset.instance = self.object
teamroster_formset.save()
I have no idea what is going and what I should do... Any ideas please?
Thanks, Ara
Upvotes: 0
Views: 1115
Reputation: 375
Well dumb me... Just had to remove the commit=false from the .save() method -.-
Upvotes: 1