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})
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.
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() .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With