简体   繁体   中英

Django - Trying to a field that is not in the form but is in the model

Right now I have a model.form with one field nothing more, but the model has 3 fields(see reference down below) and 1 of them set by the form, one has a default to false as it should be and the last one I will set in the view but it won't correctly do it and idk why.

Model & Form.

class TeamMembership(models.Model):
    user = models.ForeignKey(User)
    team = models.ForeignKey(Team)
    leader = models.BooleanField(default=False)

class TeamSettings_acceptForm(forms.ModelForm):
    class Meta:
        model = TeamMembership
        fields = ('user',)

View

@login_required
def teamsettings_accept_applications(request, team_pk):
    if request.method == 'POST':
        logged_in_user = get_object_or_404(User, pk=request.user.pk)
        requested_team = get_object_or_404(Team, pk=team_pk)
        for member in requested_team.teammembership_set.all().order_by('-leader'):
            if member.user.pk == request.user.pk and member.leader:
                formaccept = TeamSettings_acceptForm(request.POST)
                accepteduserid = formaccept.data['user']
                teamapplications = TeamApplication.objects.all().filter(from_user=accepteduserid).count()
                if teamapplications > 1:
                    messages.success(request, "Error")
                    return redirect('teamsettings_applications', team_pk=team_pk)
                else:
                    if formaccept.is_valid():
                        teamapplications = TeamApplication.objects.all().filter(from_user=accepteduserid)
                        teamapplications.update(accepted=True)
                        formaccept.team = requested_team.pk
                        formaccept.save()
                        messages.success(request, "User has now been added to your team!")
                        return redirect('teamsettings_applications', team_pk=team_pk)

it should create a new row with that data and update the others.

All I get in return from Django is

staff_teammembership.team_id may not be NULL

You haven't processed the Model form into an instance of a model yet. So formaccept doesn't know what team is here, because it's an instance of TeamSettings_acceptForm which doesn't have team as a field. To fix this change the is_valid code:

if formaccept.is_valid():
    teamapplications = TeamApplication.objects.all().filter(from_user=accepteduserid)
    teamapplications.update(accepted=True)

    # New code here
    new_team_membership = formaccept.save(commit=false)
    new_team_membership.team = requested_team.pk
    new_team_membership.save()

    messages.success(request, "User has now been added to your team!")
    return redirect('teamsettings_applications', team_pk=team_pk)

Using commit=false is really handy with Modelforms.

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