Search code examples
djangodjango-modelsdjango-formsmany-to-many

how to set default values for manytomanyfield


Is there a way around the "instance needs to have a primary key value before a many-to-many relationship can be used" error?

I have a model with several fields including a many-to-many relationship to another model. I have a function to set default values on those fields when I am creating a new instance of the model. I do this in the view when handling an HTTP GET. The m2m field gets rendered using a formset.

Here is some pseudo-code describing the situation:

class MyRelatedModel(models.Model):
  name = models.CharField(max_length=100,blank=True)

class MyModel(models.Model):
  name = models.CharField(max_length=100,blank=True)
  relatedModels = models.ManyToManyField("MyRelatedModel")

  def initialize(self):
    self.name = "my default name"
    # this bit doesn't really matter...
    # just assume I am creating a new set of RelatedModels that I want to associate with my this new MyModel...
    newRelatedModels = []
    for name in ["related model 1", "related model 2", "related model 3"]:
      relatedModel = MyRelatedModel(name=name)
      relatedModel.save()
      newRelatedModels.append(relatedModel.id)
    self.relatedModels = newRelatedModels # this is invalid!

def MyView(request):
  if request.method == 'GET':
    model = MyModel()
    model.initialize()
    form = MyForm(instance=model)
  return render_to_response("my_template.html", {"form" : form}, context_instance=RequestContext(request))

Any suggestions?

I suspect that I may need to handle this on the form side rather than the model side, but that's confusing me too.


Solution

  • For the Form what is working for me in 1.3.1:

    is something similar to this, the manytomany in the inital

    with hardcode ids:

    def MyView(request):
        if request.method == 'GET':
            model   = MyModel( name = "my default name")
            initial = { 'relatedModels': [1,] } # assuming you knew the id and wanted to hardcode it
            form = MyForm( instance=model, initial=initial )
            return render_to_response("my_template.html", {"form" : form}, context_instance=RequestContext(request))
    

    using a query to populate by names:

    def MyView(request):
        if request.method == 'GET':
            model   = MyModel( name = "my default name")
            initial = { 'relatedModels':  [ o.pk for o in list(MyRelatedModel.objects.filter( name__in=("related model 1", "related model 2", "related model 3")))], } 
            form = MyForm( instance=model, initial=initial )
            return render_to_response("my_template.html", {"form" : form}, context_instance=RequestContext(request))