Friday, October 28, 2011

Re: create object instance with ManyToManyField

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 <jaroslav.dobrek@gmail.com>:
> 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
>
> --
> 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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment