Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting queryset for foreign key for inline formset in Django

I have build a program (using Django 1.9) to track tournaments. Each tournament consists of a series of bouts, and each bout has two people (combatants) associated with it.

A tournament has a 'combatant_pool', which contains a subset of all combatant objects. The interface currently allows me to add/remove combatants from 'combatant_pool'.

The issue is with the screen that allows me to record bouts. I can view/add/remove/modify bouts without issue, but the problem with entering bouts is that the dropdowns that allows me to choose 'combatant_1' and 'combatant_2' allow me to choose from ANY combatant in the database, and what I need is for only the combatants in 'combatant_pool' to be listed.

I have looked through many other forums related to this issue, but none seem to help me solve this problem.

class combatant(models.Model):
    first_name = models.CharField(max_length=100)

class tournament(models.Model):
    combatant_pool = models.ManyToManyField(combatant, blank=True)

class bout(models.Model):
    parent_tournament = models.ForeignKey(tournament, on_delete=models.CASCADE)
    combatant_1 = models.ForeignKey(combatant, on_delete=models.CASCADE, related_name='combatant1')
    combatant_2 = models.ForeignKey(combatant, on_delete=models.CASCADE, related_name='combatant2')
    outcome = models.CharField(max_length=10)   


def BoutsView(request, pk):
    ThisTournament = tournament.objects.get(id=pk)

    BoutInlineFormSet = inlineformset_factory(tournament, bout, fields=('combatant_1', 'outcome', 'combatant_2'), formset=BaseInlineFormSet)

    if request.method == "POST":
        formset = BoutInlineFormSet(request.POST, request.FILES, instance=ThisTournament)
        if formset.is_valid():
            formset.save()
            # Do something.
            return HttpResponseRedirect('/TournamentTracker/' + str(pk) + '/bouts')
    else:
        formset = BoutInlineFormSet(instance=ThisTournament)

    return render(request, 'tournament_bouts_update_form.html', {'formset': formset, 'pk': pk})
like image 569
Ben Avatar asked Jan 04 '16 04:01

Ben


People also ask

How does ForeignKey work in Django?

What is ForeignKey in Django? ForeignKey is a Field (which represents a column in a database table), and it's used to create many-to-one relationships within tables. It's a standard practice in relational databases to connect data using ForeignKeys.

What is meant by Instance in Django?

Creating objects To create a new instance of a model, instantiate it like any other Python class: class Model (**kwargs) The keyword arguments are the names of the fields you've defined on your model. Note that instantiating a model in no way touches your database; for that, you need to save() .


1 Answers

Ah, finally found a solution that works.

I just had to add this method to the view:

    def get_field_qs(field, **kwargs):
        if field.name in ['combatant_1', 'combatant_2']:
            return forms.ModelChoiceField(queryset=Tournament.objects.get(id=pk).combatant_pool)
        return field.formfield(**kwargs)

and then add the parameter

formfield_callback=get_field_qs

to the inlineformset_factory call.

like image 152
Ben Avatar answered Nov 02 '22 08:11

Ben