get a multiple choice queryset in Django view and

2019-08-19 04:19发布

问题:

I have a multiple choice field with a foreign key. I want to save which keeper was attending a training session and I want to list all keepers as a multiple choice field.

class AddAttendance(forms.ModelForm):
attendanceKeeper = Attendance.objects.only("keeper","present").all()
keeperValues = Attendance.objects.values_list("keeper__id", flat=True).distinct()
keeper = forms.ModelMultipleChoiceField(widget=forms.widgets.CheckboxSelectMultiple, queryset=Keeper.objects.filter(id__in=keeperValues, status=1))

class Meta:
    model = Attendance
    fields = ('keeper',)

def __init__(self, *args, **kwargs):
    super(AddAttendance, self).__init__(*args, **kwargs)
    self.initial["keeper"] = Keeper.objects.all()

However my problem is, I am not familiar how to handle a queryset in the view and how to loop through it and to save every instance with the value True or False.

I always get the value error that a queryset cannot be assigned

"Attendance.keeper" must be a "Keeper" instance

Can you help me how I access the queryset values and save them

def new_attendance(request, team_pk, package_pk):
if request.method == "POST":
    form = AddAttendance(request.POST)
    if form.is_valid():
        for item in form:
            attendance = item.save(commit=False)
            attendance.keeper = get_object_or_404(AddAttendance.keeper)
            attendance.team = get_object_or_404(Team, pk=team_pk)
            attendance.created_date = timezone.now()
            attendance.save()
            return redirect(reverse('select_package', args=[package_pk, team_pk]))
else:
    form = AddAttendance()
return render(request, 'attendance/new_attendance.html', {'form': form})

In the end I want to match keeper from the queryset and save True/False into the field present in my model

class Attendance(models.Model):
session = models.ForeignKey(Session)
keeper = models.ForeignKey(Keeper)
team = models.ForeignKey(Team)
present = models.BooleanField()
created_date = models.DateTimeField(default=timezone.now)
edited_date = models.DateTimeField(default=timezone.now)

回答1:

You don't want a multiple choice field; you want a single choice. Only one keeper can be associated with each Attendance object.

You are doing a bunch of strange and unnecessary things here. You should remove most of this code, and use the ModelChoiceField which is the default for a ForeignKey. You don't want a checkbox widget either, since again that is for multiple choices; perhaps a radiobutton would be suitable.

class AddAttendance(forms.ModelForm):
    class Meta:
        model = Attendance
        fields = ('keeper',)
        widgets = {'keeper': forms.RadioSelect}
    # remove the __init__ and the field definitions, you don't need them

...

form = AddAttendance(request.POST)
if form.is_valid():
    attendance = item.save(commit=False)
    attendance.team = get_object_or_404(Team, pk=team_pk)
    attendance.created_date = timezone.now()
    attendance.save()
    return redirect(reverse('select_package', args=[package_pk, team_pk]))

There's no need to set the keeper explicitly in the view, since that's what the form is doing.