Hi Jaroslav, you have to do the following
n = Company(name=my_name, country=my_country, isin=my_isin) n.save() n.indices.add(my_indices) See here https://docs.djangoproject.com/en/dev/topics/db/queries/#saving-foreignkey-and-manytomanyfield-fields Bye 2011/10/27 Jaroslav Dobrek <jaroslav.dob...@gmail.com>: > Hello, > > how can I create an object instance with a ManyToManyField. Everything > works fine via the admin interface. But I don't understand how to do > it in Python. > > Example: I have this model: > > class Company(models.Model): > > name = models.CharField(max_length=200, unique=True) > country = models.ForeignKey(Country) > isin = models.CharField(max_length=12, blank=True) > indices = models.ManyToManyField(Index, blank=True) > > def the_indices(self): > ind = [] > for index in self.indices.all(): > ind.append(index.name) > return ', '.join(ind) > the_indices.short_description = u'Indices' > > def __unicode__(self): > return self.name > > This will work: > > n = Company(name=my_name, country=my_country, isin=my_isin) > n.save() > > This will not work: > > n = Company(name=my_name, country=my_country, isin=my_isin, > indices=my_indices) > n.save() > > Although I make sure that my_indices is a list of existing index > objects. > > What do I have to do? > > Jaroslav > > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.