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 : > 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+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.
Re: create object instance with ManyToManyField
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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: create object instance with ManyToManyField
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 : > 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.
Re: create object method
The Boss wrote: that was needed. one problem with django i guess is figuring out what exactly are the correct names and hierarchy, Precisely, which is why the magic-removal branch has changed this particular annoyance (model classes now don't jump to a different namespace) and a few others. I had enough problems dealing with the magic that I went ahead and moved my development to magic-removal... See http://code.djangoproject.com/wiki/RemovingTheMagic -- --Max Battcher-- http://www.worldmaker.net/ "History bleeds for tomorrow / for us to realize and never more follow blind" --Machinae Supremacy, Deus Ex Machinae, Title Track
Re: create object method
thanks Joseph! that got me on the right track. it wasn't quite... from django.models.mypp.group import Group but... from django.models.mypp import groups that was needed. one problem with django i guess is figuring out what exactly are the correct names and hierarchy, but i just took the clue from the import line in tutorial 1 for the interactive shell and imported the module 'groups', since I guess Group is actually just the name of the class def, and the latter version seems more consistent with what I see elsewhere in django. But most important i didn't realize i could import this directly into a method of this class that will become a module. thanks again.
Re: create object method
On 1/14/06, The Boss <[EMAIL PROTECTED]> wrote: > > but when I try to put a check into add_related that says to create a > new group if the person doesn't yet have one > (by using > g=group.Group(name='x') > g.save() > > it tells me that global name groups is not defined, despite having > defined Group class right above People class, and despite the fact that > g=group.Group(name='x') works in the python command line. For now the easyist way to do this is to import Group in your method body. For example: class Person(meta.Model): # attributes, etc. def add_related(self): from django.models.mypp.group import Group g=group.Group(name='x') g.save() Django currently does some magic stuff in the models that doesn't allow imports to work as you'd expect them to. This will be fixed soon. Probably in 0.92. Search for module_constants on http://www.djangoproject.com/documentation/model_api/ for another option. Joseph
Re: create object
Hello Grigory Fateyev! On Thu, 27 Oct 2005 19:43:45 +0400 you wrote: > > Hello! > > http://django.pastebin.com/407800 > > Somebody can explane why this app could not create object? Any advice > ... > Problem was fixed! -- Всего наилучшего! greg [at] anastasia [dot] ru Григорий.