create object instance with ManyToManyField

2,351 views
Skip to first unread message

Jaroslav Dobrek

unread,
Oct 27, 2011, 12:26:38 PM10/27/11
to Django users
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




Leonardo Giordani

unread,
Oct 27, 2011, 12:39:48 PM10/27/11
to django...@googlegroups.com
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 <jarosla...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Jaroslav Dobrek

unread,
Oct 28, 2011, 8:11:34 AM10/28/11
to Django users
Hello Leonardo,

thanks your your answer.

> n = Company(name=my_name, country=my_country, isin=my_isin)
> n.save()
> n.indices.add(my_indices)

This causes that the company object has all indices in my_indices
(such as Dow Jones S&P 100, Dax, ...) as *choices*. I.e. someone who
manipulates these objects via the admin interface is now able to
choose one or several of them and save the object again. What I want
to do is choose one or several of the indices in my program and then
save the object.

Jaroslav

Leonardo Giordani

unread,
Oct 28, 2011, 8:39:42 AM10/28/11
to django...@googlegroups.com
The add() method of a ManyToMany field adds the indexes of the given
objects to the field.
If you add a list, each index in the list is added to the field.

In the admin interface, when you edit a Company object, you see a
convenient automagically-created
menu which lists all Index object in your application, and there you
can add or remove them.

But you can add Indexes with the snipped I gave you; if you pass as
"my_indices" some of the
available Indexes and then edit the object through the admin interface
you'll see the ones you added
as selected (in blue).

*Choices* in the admin interface are build through an automatic query;
Django sees that you use a m2m field
and populates it with TheTypeYouReference.objects.all() . But this is
a service of the forms in the admin interface.
Django objects have nothing to do with "choices", they simply know
that you are attaching the index of some
other object to the object you are creating.

Feel free to ask again if the matter is not clear.


2011/10/28 Jaroslav Dobrek <jarosla...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages