Running sync_to_async to create object, but YET raise .ASynchronous exception
Hello guys! I'm stucked on this problem, i have some script to save data to database using a django object. The script is with async,so i have to use the 'sync_to_async' function. Beyond that, running the script raise the ASynchronous exception, but it's look correct. Code: ``` async def cycle_send_to_dj(self, *args): """ Leer la cola que recibe de rmq Enviar cada dato recibido de la cola y cargarlo """ queue = args[0] data = [] try: #breakpoint() async for elem in read_async_queue(queue): gprint("Recibiendo data en async queue") rprint(elem) # transformar elem para que sea guardable try: data_log = await statusdata2log( elem.get("station"), **elem.get("data")) if data_log: try: bprint("="*20+">") gprint("Data log") rprint(data_log) bprint("ready to build") breakpoint() value = await sync_to_async(LogUnidadGNSS)(**data_log) rprint(value) data.append(value) except Exception as e: bprint("Creando lista de logs, error %s, LogUnidadGNSS create" % e) raise e except Exception as e: print("Error al transformar elem %s" % e) gprint("Data to save") [print(elem) for elem in data] if data: #await dbaccess(LogUnidadGNSS.objects.bulk_create)(data) await dbaccess(LogUnidadGNSS.objects.bulk_create)(data) except Exception as e: print("Hubo una excepción al guardar los datos de LogGNSS") raise e return args ``` I'm worng in somethinkg? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/70ef401a-636d-4464-a912-a3f004e2de3a%40googlegroups.com.
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.
create object instance with ManyToManyField
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.
Re: Newbie Django 1.0 help with create object, uopdate object
Thanks Tim for your detailed reply. I have it now. HTML was incorrect, and yes, now I understand better what is happening I have rewritten the view as you suggest. I split it up only to try to make clearer to me what was happening. Thanks again. Mike On Jun 2, 1:48 am, Tim Sawyer wrote: > Hi, > > I think the recommended approach in django is for forms to submit to the > current URL and for the view to decide whether the request is to display the > form (a HTTP GET) or submit the form (HTTP POST). I'd suggest that you > attempt to re-write this way. > > Your error could be caused by nothing in the POST, if your form is wrong - can > you post your HTML? You could also put an intentional error into your view > which will cause the Django error window to pop up - this contains details of > the POST variables in the request. > > Here's a well commented and cleaner version of the code I posted earlier: > > def edit_result(request, pResultSerial): > """ > This method in views.py takes a single parameter from the url - the serial of > an object. It will either display the HTML form to edit that object, or it > will accept the submit of a form containing the edited data and save it to the > database > """ > # Get an object from the database, using the passed in serial. Raise a 404 > # page not found if the object isn't found > lContestResult = get_object_or_404(ContestResult, pk=pResultSerial) > > # if we are doing a post, the we have data to save. Process it. > if request.method == 'POST': > > # create a form instance, populating it with the data in the object > # selected from the database earlier, then overwriting it with the > # stuff submitted in the HTML form > form = ContestResultForm(request.POST, instance=lContestResult) > > # run the form validation > if form.is_valid(): > # save the object inside the form instance to the database > form.save() > > # our save completed, so redirect to the next url you want to go to > return HttpResponseRedirect('/url/after/successful/save') > > else: > # we aren't doing a POST, so we need to create a form > # instance ready for it to be edited in the HTML. > # we create this and populate it from the object we selected above > form = ContestResultForm(instance=lContestResult) > > # show the HTML form to allow the user to edit the object > # note that this is indented to the same level as the > # "if request is a POST" check, so that if the form.is_valid() check fails, > # we go back and show the HTML again with the form containing errors. > return render__to_response('contests/edit_result.html', > {'form': form}) > > Hope that helps! > > Tim. > > On Monday 01 June 2009 15:56:24 adelaide_mike wrote: > > > Thanks for your response Tim. However, you lost me a bit there, I am > > a real newbie. I have narrowed my question down to this: > > > # in views.py: > > > class PropertyForm(ModelForm): > > class Meta: > > model = Property > > > def property_update(request, property_id='0', street_id='0'): > > print "data/property_save, request.method= ", request.method > > message = '' > > # we attempt to update an edit > > print "attempt to update" > > form = PropertyForm(request.POST) > > if form.is_valid(): > > form.save() > > > return render_to_response('wha/property_form.html', {'form': form, > > 'message': message}) > > > My property_update function is called when the form Save button is > > clicked. The various "print" commands operate as expected. However, > > the validation fails and a form with no data is returned with > > "required data" labels. I conclude the line: > > form = PropertyForm(request.POST) > > does not populate the validation form. What have I got wrong here? > > TIA > > > Mike > > > On Jun 1, 8:14 pm, Tim Sawyer wrote: > > > On Monday 01 June 2009 01:38:30 adelaide_mike wrote: > > > > I found a really clear explanation of creating and updating database > > > > objects in SAMS TeachYourself Django, but it appears to be for v > > > > 0.96. > > > > > I have looked at "Creating forms from models" in the documentation, > > > > and about one-third the way down it shows the following: > > > > > # Create a form instance from POST data. > > > > > >>> f = ArticleForm(request.POST) > > > > > # Save a new Article object from the form's data. > > > > > >>> new_article = f.save() > > > > > # Create a form to edit an existing Article. > > > > > >>> a = Article.objects.get(pk=1) > > > > >>> f = ArticleForm(instance=a) > > > > >>> f.save() > > > > > # Create a form to edit an existing Article, but use > > > > # POST data to populate the form. > > > > > >>> a = Article.objects.get(pk=1) > > > > >>> f = ArticleForm(request.POST, instance=a) > > > > >>> f.save() > > > > > I understand what these code fragments are intended to do (I think) > > > > but I am not clear as to how to use them. Can someo
Re: Newbie Django 1.0 help with create object, uopdate object
Hi, I think the recommended approach in django is for forms to submit to the current URL and for the view to decide whether the request is to display the form (a HTTP GET) or submit the form (HTTP POST). I'd suggest that you attempt to re-write this way. Your error could be caused by nothing in the POST, if your form is wrong - can you post your HTML? You could also put an intentional error into your view which will cause the Django error window to pop up - this contains details of the POST variables in the request. Here's a well commented and cleaner version of the code I posted earlier: def edit_result(request, pResultSerial): """ This method in views.py takes a single parameter from the url - the serial of an object. It will either display the HTML form to edit that object, or it will accept the submit of a form containing the edited data and save it to the database """ # Get an object from the database, using the passed in serial. Raise a 404 # page not found if the object isn't found lContestResult = get_object_or_404(ContestResult, pk=pResultSerial) # if we are doing a post, the we have data to save. Process it. if request.method == 'POST': # create a form instance, populating it with the data in the object # selected from the database earlier, then overwriting it with the # stuff submitted in the HTML form form = ContestResultForm(request.POST, instance=lContestResult) # run the form validation if form.is_valid(): # save the object inside the form instance to the database form.save() # our save completed, so redirect to the next url you want to go to return HttpResponseRedirect('/url/after/successful/save') else: # we aren't doing a POST, so we need to create a form # instance ready for it to be edited in the HTML. # we create this and populate it from the object we selected above form = ContestResultForm(instance=lContestResult) # show the HTML form to allow the user to edit the object # note that this is indented to the same level as the # "if request is a POST" check, so that if the form.is_valid() check fails, # we go back and show the HTML again with the form containing errors. return render__to_response('contests/edit_result.html', {'form': form}) Hope that helps! Tim. On Monday 01 June 2009 15:56:24 adelaide_mike wrote: > Thanks for your response Tim. However, you lost me a bit there, I am > a real newbie. I have narrowed my question down to this: > > # in views.py: > > class PropertyForm(ModelForm): > class Meta: > model = Property > > def property_update(request, property_id='0', street_id='0'): > print "data/property_save, request.method= ", request.method > message = '' > # we attempt to update an edit > print "attempt to update" > form = PropertyForm(request.POST) > if form.is_valid(): > form.save() > > return render_to_response('wha/property_form.html', {'form': form, > 'message': message}) > > My property_update function is called when the form Save button is > clicked. The various "print" commands operate as expected. However, > the validation fails and a form with no data is returned with > "required data" labels. I conclude the line: > form = PropertyForm(request.POST) > does not populate the validation form. What have I got wrong here? > TIA > > Mike > > On Jun 1, 8:14 pm, Tim Sawyer wrote: > > On Monday 01 June 2009 01:38:30 adelaide_mike wrote: > > > I found a really clear explanation of creating and updating database > > > objects in SAMS TeachYourself Django, but it appears to be for v > > > 0.96. > > > > > > I have looked at "Creating forms from models" in the documentation, > > > and about one-third the way down it shows the following: > > > > > > # Create a form instance from POST data. > > > > > > >>> f = ArticleForm(request.POST) > > > > > > # Save a new Article object from the form's data. > > > > > > >>> new_article = f.save() > > > > > > # Create a form to edit an existing Article. > > > > > > >>> a = Article.objects.get(pk=1) > > > >>> f = ArticleForm(instance=a) > > > >>> f.save() > > > > > > # Create a form to edit an existing Article, but use > > > # POST data to populate the form. > > > > > > >>> a = Article.objects.get(pk=1) > > > >>> f = ArticleForm(request.POST, instance=a) > > > >>> f.save() > > > > > > I understand what these code fragments are intended to do (I think) > > > but I am not clear as to how to use them. Can someone point me to a > > > more fully displayed example? TIA > > > > > > Mike > > > > Here's an example from my code, does this help? > > > > Tim. > > > > def edit_result(request, pResultSerial): > > """ > > Edit a single result row > > """ > > lContestResult = get_object_or_404(ContestResult, pk=pResultSerial) > > if request.user != lContestResult.owner: > > raise Http404() > >
Re: Newbie Django 1.0 help with create object, uopdate object
Thanks for your response Tim. However, you lost me a bit there, I am a real newbie. I have narrowed my question down to this: # in views.py: class PropertyForm(ModelForm): class Meta: model = Property def property_update(request, property_id='0', street_id='0'): print "data/property_save, request.method= ", request.method message = '' # we attempt to update an edit print "attempt to update" form = PropertyForm(request.POST) if form.is_valid(): form.save() return render_to_response('wha/property_form.html', {'form': form, 'message': message}) My property_update function is called when the form Save button is clicked. The various "print" commands operate as expected. However, the validation fails and a form with no data is returned with "required data" labels. I conclude the line: form = PropertyForm(request.POST) does not populate the validation form. What have I got wrong here? TIA Mike On Jun 1, 8:14 pm, Tim Sawyer wrote: > On Monday 01 June 2009 01:38:30 adelaide_mike wrote: > > > > > I found a really clear explanation of creating and updating database > > objects in SAMS TeachYourself Django, but it appears to be for v > > 0.96. > > > I have looked at "Creating forms from models" in the documentation, > > and about one-third the way down it shows the following: > > > # Create a form instance from POST data. > > > >>> f = ArticleForm(request.POST) > > > # Save a new Article object from the form's data. > > > >>> new_article = f.save() > > > # Create a form to edit an existing Article. > > > >>> a = Article.objects.get(pk=1) > > >>> f = ArticleForm(instance=a) > > >>> f.save() > > > # Create a form to edit an existing Article, but use > > # POST data to populate the form. > > > >>> a = Article.objects.get(pk=1) > > >>> f = ArticleForm(request.POST, instance=a) > > >>> f.save() > > > I understand what these code fragments are intended to do (I think) > > but I am not clear as to how to use them. Can someone point me to a > > more fully displayed example? TIA > > > Mike > > Here's an example from my code, does this help? > > Tim. > > def edit_result(request, pResultSerial): > """ > Edit a single result row > """ > lContestResult = get_object_or_404(ContestResult, pk=pResultSerial) > if request.user != lContestResult.owner: > raise Http404() > if request.method == 'POST': > form = ContestResultForm(request.POST, instance=lContestResult) > if form.is_valid(): > form.save() > return > HttpResponseRedirect(reverse('bbr.contests.views.single_contest_event', > args=[lContestResult.contest_event.contest.slug, > lContestResult.contest_event.date_of_event])) > else: > form = ContestResultForm(instance=lContestResult) > > return render_auth(request, 'contests/edit_result.html', {'form': form, > 'ContestResult' : lContestResult}) --~--~-~--~~~---~--~~ 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: Newbie Django 1.0 help with create object, uopdate object
On Monday 01 June 2009 01:38:30 adelaide_mike wrote: > I found a really clear explanation of creating and updating database > objects in SAMS TeachYourself Django, but it appears to be for v > 0.96. > > I have looked at "Creating forms from models" in the documentation, > and about one-third the way down it shows the following: > > # Create a form instance from POST data. > > >>> f = ArticleForm(request.POST) > > # Save a new Article object from the form's data. > > >>> new_article = f.save() > > # Create a form to edit an existing Article. > > >>> a = Article.objects.get(pk=1) > >>> f = ArticleForm(instance=a) > >>> f.save() > > # Create a form to edit an existing Article, but use > # POST data to populate the form. > > >>> a = Article.objects.get(pk=1) > >>> f = ArticleForm(request.POST, instance=a) > >>> f.save() > > I understand what these code fragments are intended to do (I think) > but I am not clear as to how to use them. Can someone point me to a > more fully displayed example? TIA > > Mike Here's an example from my code, does this help? Tim. def edit_result(request, pResultSerial): """ Edit a single result row """ lContestResult = get_object_or_404(ContestResult, pk=pResultSerial) if request.user != lContestResult.owner: raise Http404() if request.method == 'POST': form = ContestResultForm(request.POST, instance=lContestResult) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('bbr.contests.views.single_contest_event', args=[lContestResult.contest_event.contest.slug, lContestResult.contest_event.date_of_event])) else: form = ContestResultForm(instance=lContestResult) return render_auth(request, 'contests/edit_result.html', {'form': form, 'ContestResult' : lContestResult}) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Newbie Django 1.0 help with create object, uopdate object
I found a really clear explanation of creating and updating database objects in SAMS TeachYourself Django, but it appears to be for v 0.96. I have looked at "Creating forms from models" in the documentation, and about one-third the way down it shows the following: # Create a form instance from POST data. >>> f = ArticleForm(request.POST) # Save a new Article object from the form's data. >>> new_article = f.save() # Create a form to edit an existing Article. >>> a = Article.objects.get(pk=1) >>> f = ArticleForm(instance=a) >>> f.save() # Create a form to edit an existing Article, but use # POST data to populate the form. >>> a = Article.objects.get(pk=1) >>> f = ArticleForm(request.POST, instance=a) >>> f.save() I understand what these code fragments are intended to do (I think) but I am not clear as to how to use them. Can someone point me to a more fully displayed example? TIA Mike --~--~-~--~~~---~--~~ 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: what's the correct way to create object with ForeignKey?
Use either e = Subgroup.objects.get(subGroupName="p1sub1") e.save() m = User(name="xxx", subGroup=e) m.save() or e = Subgroup.objects.get(subGroupName="p1sub1") e.save() e.user_set.create(name="xxx") or e = Subgroup.objects.get(subGroupName="p1sub1") e.save() m = User(name="xxx") m.save() e.user_set.add(m) Good luck! Aidas Bendoraitis [aka Archatas] On 11/8/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > I know this maybe very simple, but I can't find the answers on the net, > I'm using following method to create object with ForeignKey, this > however works, > but according to django document you should avoid using "_id" column, > what's the correct way to do this? any idea? (running django 0.95) > > = > in shell/view: > > m = User() > e = Subgroup.objects.get(subGroupName="p1sub1") > > m.name="xxx" > m.subGroup_id=e.subGroupName > m.save() > > = > model.py: > > class User(models.Model): > name = models.CharField(maxlength=255,primary_key=True) > subGroup = models.ForeignKey('Subgroup') > . > > class Subgroup(models.Model): > subGroupName = models.CharField(maxlength=255,primary_key=True) > . > > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
what's the correct way to create object with ForeignKey?
I know this maybe very simple, but I can't find the answers on the net, I'm using following method to create object with ForeignKey, this however works, but according to django document you should avoid using "_id" column, what's the correct way to do this? any idea? (running django 0.95) = in shell/view: m = User() e = Subgroup.objects.get(subGroupName="p1sub1") m.name="xxx" m.subGroup_id=e.subGroupName m.save() = model.py: class User(models.Model): name = models.CharField(maxlength=255,primary_key=True) subGroup = models.ForeignKey('Subgroup') . class Subgroup(models.Model): subGroupName = models.CharField(maxlength=255,primary_key=True) . --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] 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
create object method
I am trying to do something really simple and I am sure there are 10 ways to do it but I can't seem to find one. I just want to include a method in one class to create a new object in another class. So I have a people class with a foreign key field to a group class. I made some methods to get_related : get all the people in the persons group and rem_related : remove people from a persons group and add_related: add people to a persons group, 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. I thought maybe I was supposed to use a manipulator object instead but thats not clear and seems unnecessary, or maybe somehow tell People that Group is defined right above it, but thats not indicated anywhere either. Does anyone have any idea whats wrong?
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 Григорий.
create object
Hello! http://django.pastebin.com/407800 Somebody can explane why this app could not create object? Any advice ... -- Всего наилучшего! greg [at] anastasia [dot] ru Григорий.