I am trying to create a model instance that includes a list of model via many-to-many field via ModelForm. However when this occurs, I need to be able to change one of the fields being referenced.
Here is the stripped code:
class AllAtt(models.Model):
weighting = models.IntegerField(null=True,blank=True)
mass = models.IntegerField(null=True,blank=True)
required_Power = models.IntegerField(null=True,blank=True)
memory = models.IntegerField(null=True,blank=True)
class Mission(AllAtt):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Scenario(models.Model):
name = models.CharField(max_length=50)
creation_date = models.DateTimeField(default=datetime.now)
creator = models.ForeignKey(User,null=True,blank=True)
mission = models.ManyToManyField(Mission)
def __unicode__(self):
return self.name
class ScenarioForm(ModelForm):
class Meta:
model = Scenario
fields = ('name','mission',)
Where the Missions instances are imported from a database. Currently this allows me to pick Mission instances from the multiplechoicefield, however this isn’t what I need. I want to be able to include all of the Mission instances in the Scenario and then change the “weighting” field through the form.
A simple example of what I am trying to do:
I have a certain number of tasks (Missions) and when combined they accomplish something (Scenario).
A human has a lot of task functions. If my Scenario is “Getting ready for a date” then these functions may be weighted in importance as:
Make coffee – 0, Feed dog -2, Shower – 3, Get dressed – 5. Whereas if the Scenario is “Working on Django code” these functions are weighted as: Make coffee – 5, Feed dog – 2, Shower – 0, Get dressed – 1. And each time I make a new Scenario I need to be able to change the weights of all the possible tasks.
Any help or insight on how to accomplish this would be greatly appreciated. Thanks!
As I understand you need to specify weight on
Missionrelations inScenario.Basically what you need is
throughModel (with weight). You can see it documented here: https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany