form queryset: queries spanning multiple relationships

Date: April 10th 2016
Last updated: April 10th 2016

What if I want to limit the options in a dropdown (multi choice field) box thats based across multiple relationships?

In this example I have three models including Board, Surfer and SurfDiary. A surfer can add a board to the database which also saves their user id (the method to do this is not shown). But they can also select existing boards via the many to many relationship. When I a user adds an entry into their SurfDiary I want to provide a queryset that only contains boards they have either selected or added to the database.

EDIT: This only works if at least one entry has been created by the user. Not sure why this is the case?? I will return to this later... I was over thinking the filter step. Now updated. Stop the filtering at the Surfer model.

class Board(models.Model):
    user = models.ForeignKey(User, blank=True, null=True)
    boardname = models.CharField('Board name', max_length=40, null=True, blank=True)

class Surfer(models.Model):
    user = models.OneToOneField(User)
    boards = models.ManyToManyField(Board)

class SurfDiary(models.Model):
    user = models.ForeignKey(User, blank=True, null=True) 
    board = models.ForeignKey(Board)

def diaryentry(request, surfer_id):
    surfer = get_object_or_404(Surfer, pk=surfer_id)
    if request.method == "POST":

        # Solution ================
        # Add user argument to form
        form = SurfDiaryForm(user=request.user)

        if form.is_valid():
            diary =
            diary.user = get_object_or_404(User,
            return redirect('/{}/diary'.format(surfer_id))
        # Solution ================
        # Add user argument to form 
        form = SurfDiaryForm(user=request.user)

    return render(request, 'surferprofile/adddiaryentry.html',
            {'surfer': surfer, 'form': form})

class SurfDiaryForm(forms.ModelForm):
    class Meta:
        model = SurfDiary
        fields = [
                 #<- snipped ->,
                 #<- snipped ->]
        exclude = ('user', )

    def __init__(self, user=None, **kwargs):
        super(SurfDiaryForm, self).__init__(**kwargs)
        if user:

            #================ Solution ======================
            # THIS FAILED
            #self.fields['board'].queryset =
            #                    Board.objects.all().filter(

            # This filter looks at the 'boards' field in the 
            # Surfer model (which is a many-to-many relationship 
            # with the Board model) and associates the user field
            # with the that I added to 'form' in 

            # NEW SOLUTION
            self.fields['board'].queryset =

results matching ""

    No results matching ""