Here is the riddle. This is a simplified version of the problem I am dealing with, just so we can capture the core problem. So, for the sake of simplicity and relevance, not all fields and relationships are shown here.
So, I have a modelformset and I would like to access each individual form to change the field based on the queryset.
class PlayerType(models.Model):
type = models.CharField(max_length=30, choices = PLAYER_TYPES)
class Player(models.Model):
name = models.CharField(max_length=30, blank=False, null=False)
player_type = models.ForeignKey(PlayerType, related_name ='players')
contract_price = models.DecimalField(max_digits = 10, decimal_places = 2, blank = False, null = False)
price_unit_of_measurement = models.CharField(max_length=20, choices=STANDARD_UOM)
Forms.py
class PlayerForm(ModelForm):
class Meta:
fields = ['name','price_unit_of_measurement']
views.py
PlayerFormSet = modelformset_factory(Player, form = PlayerForm, extra = 5)
Now, say I want to display a different unit of measurement depending on which player I am showing. For instance, player 1's contract may be based on lump sump or amount per game, and another player's contract may be based on number of minutes played, price per month, etc. depending on the player type.
In essence, I would like to know how to access each form in a modelformset and change the unit of measurement for that form alone, from the default set in the model. I also understand that inlinemodelformset is more appropriate for this application as Player related to PlayerType on one-to-many relationship basis.