Using South HOWTO create an instance of a model from a different app during DataMigration
(sorry for duplicate I have already posted on stackoverflow.com > http://goo.gl/I7Jj6 ) I need to perform a datamigration of a model Answer in app Question. In that script there is a dependency such that I need to create an instance of a model Chapter which is in the app Journal. So, I coded it as follows: def forwards(self, orm): for answer_object in orm.Answer.objects.all(): #This Works. blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[: 100]) blog.save() #This DOES NOT work chapter, is_created = orm['journal.Chapter'].objects.get_or_create(content_object=blog) chapter.save() #cleanup task, not relevant to this question below answer_object.chapter_ptr = chapter answer_object.save() But as expected this throws an error on " orm['journal.Chapter'].objects.get_or_create(content_object=blog)" saying that > django.core.exceptions.FieldError: Cannot resolve keyword 'content_object' into field. This is presumably due to content_object being a GenericForeignKey so some operations are not allowed. But I also tried other alternatives for creating the "chapter" object like, chapter = orm['journal.Chapter'](content_object=blog) ERROR > TypeError: 'content_object' is an invalid keyword argument for this function and chapter = orm.journal.Chapter(content_object=blog) ERROR > AttributeError: The model 'journal' from the app 'questions' is not available in this migration. (Did you use orm.ModelName, not orm['app.ModelName']?) So since my earlier approach was failing I tried a new tact. The model whose instantiation was failing in my code above i.e. Chapter in the Journal app, I decided to create a datamigration for that instead. I also made sure to --freeze the models I am referring to in the forwards definition. Now this should have been straight forward, I would think. I have my forward code as follows - def forwards(self, orm): for answer_object in orm['questions.Answer'].objects.all(): #Works, AGAIN! blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[: 100]) blog.save() # DOES NOT WORK, AGAIN! chapter = orm.Chapter(rank=1, content_object=blog) chapter.save() I would have thought that now since I am creating instance of a model (Chapter) which exists in the subject app (Journal) everything should have worked out. But i am getting the same error > TypeError: 'content_object' is an invalid keyword argument for this function. It fails at the same point, namely, "content_object". I will post below the model definition if that might help. class Chapter(models.Model): rank = models.IntegerField() content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() Also all the models being touched in these forwards methods, namely - blog, chapter, questions; are fully defined in the 00n_*.py files created by South's schemamigration. -- 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: problem with django admin
I think you might want to define what text to display when you directly reference the model's instance. see https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#unicode In Java it is like overriding the toString() method. On Mon, Aug 8, 2011 at 10:35 AM, Oyedamola Oyeniyi wrote: > Hi all, > > I have a strange problem with the admin pages and I don't evenknow > where to begin tolook at the FAQ. I'm a newbie to Django and Python(in > fact, web applications in general) so forgive the dumbness of the > question. > > I have just finished the models.py and admin.py files on an app I am > working with. Navigations and the like on the admin interface seems to > work fine. But I have issues with 3 tables: Operator,Address and > Operator_Address. As is obvious, I created the Operator_Address table > as a join table for the manytomany relationship between Operator, and > Address!. > > I am trying to populate the Operator_Address table with data I have > filled in Operator and Address tables respectively. I assumed that in > the dropbox for the Operator_Address interface, I was supposed to be > presented with string literals signifying the Operators and Addresses > I filled in their respective interfaces, instead I get "Operator > object" and "Address object" for as many objects I created. > > Am I missing something? > > Thanks > > -- > 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. > > Thanks, Chintan -- 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: Using South HOWTO create an instance of a model from a different app during DataMigration
Tom, Thanks for your reply. I will try it out and post back the results. On Mon, Aug 8, 2011 at 10:25 AM, Tom Evans wrote: > On Mon, Aug 8, 2011 at 3:06 PM, Chintan Tank > wrote: > > (sorry for duplicate I have already posted on stackoverflow.com > > > http://goo.gl/I7Jj6 ) > > > > I need to perform a datamigration of a model Answer in app Question. > > In that script there is a dependency such that I need to create an > > instance of a model Chapter which is in the app Journal. So, I coded > > it as follows: > > > > def forwards(self, orm): > >for answer_object in orm.Answer.objects.all(): > > > >#This Works. > >blog, is_created = > > orm['blog.Post'].objects.get_or_create(title=answer_object.answer[: > > 100]) > >blog.save() > > > >#This DOES NOT work > >chapter, is_created = > > orm['journal.Chapter'].objects.get_or_create(content_object=blog) > >chapter.save() > >#cleanup task, not relevant to this question below > >answer_object.chapter_ptr = chapter > >answer_object.save() > > > > > > > > But as expected this throws an error on " > > orm['journal.Chapter'].objects.get_or_create(content_object=blog)" > > saying that > django.core.exceptions.FieldError: Cannot resolve > > keyword 'content_object' into field. > > > > This is presumably due to content_object being a GenericForeignKey so > > some operations are not allowed. But I also tried other alternatives > > for creating the "chapter" object like, > > > > chapter = orm['journal.Chapter'](content_object=blog) > > ERROR > TypeError: 'content_object' is an invalid keyword argument for > > this function > > > > and > > > > chapter = orm.journal.Chapter(content_object=blog) > > ERROR > AttributeError: The model 'journal' from the app 'questions' > > is not available in this migration. (Did you use orm.ModelName, not > > orm['app.ModelName']?) > > > > So since my earlier approach was failing I tried a new tact. The model > > whose instantiation was failing in my code above i.e. Chapter in the > > Journal app, I decided to create a datamigration for that instead. I > > also made sure to --freeze the models I am referring to in the > > forwards definition. Now this should have been straight forward, I > > would think. I have my forward code as follows - > > > > def forwards(self, orm): > > > >for answer_object in orm['questions.Answer'].objects.all(): > > > >#Works, AGAIN! > >blog, is_created = > > orm['blog.Post'].objects.get_or_create(title=answer_object.answer[: > > 100]) > >blog.save() > > > ># DOES NOT WORK, AGAIN! > >chapter = orm.Chapter(rank=1, content_object=blog) > >chapter.save() > > > > > > I would have thought that now since I am creating instance of a model > > (Chapter) which exists in the subject app (Journal) everything should > > have worked out. But i am getting the same error > TypeError: > > 'content_object' is an invalid keyword argument for this function. > > > > It fails at the same point, namely, "content_object". I will post > > below the model definition if that might help. > > > > class Chapter(models.Model): > > > >rank = models.IntegerField() > > > >content_type = models.ForeignKey(ContentType) > >object_id = models.PositiveIntegerField() > >content_object = generic.GenericForeignKey() > > > > Also all the models being touched in these forwards methods, namely - > > blog, chapter, questions; are fully defined in the 00n_*.py files > > created by South's schemamigration. > > > > content_object isn't a 'real' field, it is some magic applied to the > ORM and your model. The real fields are object_id and content type, so > use them directly. > > Use ContentType.objects.get_for_model() [1] to get the content type. > > [1] > https://docs.djangoproject.com/en/1.3/ref/contrib/contenttypes/#django.contrib.contenttypes.models.ContentTypeManager.get_for_model > > Cheers > > Tom > > -- > 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. > > -- Thanks & Regards, Chintan Tank -- 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: Using South HOWTO create an instance of a model from a different app during DataMigration
Tom, I tried looking up the content_type and then using it, but it still throws errors. *1. I tried* ct = ContentType.objects.get_for_model(Post) chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id) *It gave error,* ValueError: Cannot assign "": "Chapter.content_type" must be a "ContentType" instance. *2. I tried* ct = orm['contenttypes.ContentType'].objects.get_for_model(orm['blog.Post']) chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id) *It gave error,* AttributeError: 'Manager' object has no attribute 'get_for_model' *3. I tried* ct = orm['contenttypes.ContentType'].get(app_label="blog", model="Post") # I also tried setting model in the above get method to be lowercase. chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id) *It gave error,* AttributeError: type object 'ContentType' has no attribute 'get' *BTW*, I am using *South 0.7.3.* Thanks again for helping. On Mon, Aug 8, 2011 at 10:25 AM, Tom Evans wrote: > On Mon, Aug 8, 2011 at 3:06 PM, Chintan Tank > wrote: > > (sorry for duplicate I have already posted on stackoverflow.com > > > http://goo.gl/I7Jj6 ) > > > > I need to perform a datamigration of a model Answer in app Question. > > In that script there is a dependency such that I need to create an > > instance of a model Chapter which is in the app Journal. So, I coded > > it as follows: > > > > def forwards(self, orm): > >for answer_object in orm.Answer.objects.all(): > > > >#This Works. > >blog, is_created = > > orm['blog.Post'].objects.get_or_create(title=answer_object.answer[: > > 100]) > >blog.save() > > > >#This DOES NOT work > >chapter, is_created = > > orm['journal.Chapter'].objects.get_or_create(content_object=blog) > >chapter.save() > >#cleanup task, not relevant to this question below > >answer_object.chapter_ptr = chapter > >answer_object.save() > > > > > > > > But as expected this throws an error on " > > orm['journal.Chapter'].objects.get_or_create(content_object=blog)" > > saying that > django.core.exceptions.FieldError: Cannot resolve > > keyword 'content_object' into field. > > > > This is presumably due to content_object being a GenericForeignKey so > > some operations are not allowed. But I also tried other alternatives > > for creating the "chapter" object like, > > > > chapter = orm['journal.Chapter'](content_object=blog) > > ERROR > TypeError: 'content_object' is an invalid keyword argument for > > this function > > > > and > > > > chapter = orm.journal.Chapter(content_object=blog) > > ERROR > AttributeError: The model 'journal' from the app 'questions' > > is not available in this migration. (Did you use orm.ModelName, not > > orm['app.ModelName']?) > > > > So since my earlier approach was failing I tried a new tact. The model > > whose instantiation was failing in my code above i.e. Chapter in the > > Journal app, I decided to create a datamigration for that instead. I > > also made sure to --freeze the models I am referring to in the > > forwards definition. Now this should have been straight forward, I > > would think. I have my forward code as follows - > > > > def forwards(self, orm): > > > >for answer_object in orm['questions.Answer'].objects.all(): > > > >#Works, AGAIN! > >blog, is_created = > > orm['blog.Post'].objects.get_or_create(title=answer_object.answer[: > > 100]) > >blog.save() > > > ># DOES NOT WORK, AGAIN! > >chapter = orm.Chapter(rank=1, content_object=blog) > >chapter.save() > > > > > > I would have thought that now since I am creating instance of a model > > (Chapter) which exists in the subject app (Journal) everything should > > have worked out. But i am getting the same error > TypeError: > > 'content_object' is an invalid keyword argument for this function. > > > > It fails at the same point, namely, "content_object". I will post > > below the model definition if that might help. > > > > class Chapter(models.Model): > > > >rank = models.IntegerField() > > > >content_type = models.ForeignKey(ContentType) > >object_id = models.PositiveIntegerField() > >c
Re: Using South HOWTO create an instance of a model from a different app during DataMigration
*UPDATE:* * * I was able to resolve the problem by looking up the contenttype manually so, ct = orm['contenttypes.ContentType'].*objects.*get(app_label="blog", model="Post") Thanks Tom & Andrew (at south user listserv) for helping with this. On Sat, Aug 13, 2011 at 1:14 PM, Chintan Tank wrote: > Tom, > I tried looking up the content_type and then using it, but it still throws > errors. > > *1. I tried* > > ct = ContentType.objects.get_for_model(Post) > chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id) > > *It gave error,* > > ValueError: Cannot assign "": "Chapter.content_type" > must be a "ContentType" instance. > > *2. I tried* > > ct = > orm['contenttypes.ContentType'].objects.get_for_model(orm['blog.Post']) > chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id) > > *It gave error,* > > AttributeError: 'Manager' object has no attribute 'get_for_model' > > *3. I tried* > > ct = orm['contenttypes.ContentType'].get(app_label="blog", model="Post") > > # I also tried setting model in the above get method to be lowercase. > > chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id) > > *It gave error,* > > AttributeError: type object 'ContentType' has no attribute 'get' > > *BTW*, I am using *South 0.7.3.* > > Thanks again for helping. > > On Mon, Aug 8, 2011 at 10:25 AM, Tom Evans wrote: > >> On Mon, Aug 8, 2011 at 3:06 PM, Chintan Tank >> wrote: >> > (sorry for duplicate I have already posted on stackoverflow.com > >> > http://goo.gl/I7Jj6 ) >> > >> > I need to perform a datamigration of a model Answer in app Question. >> > In that script there is a dependency such that I need to create an >> > instance of a model Chapter which is in the app Journal. So, I coded >> > it as follows: >> > >> > def forwards(self, orm): >> >for answer_object in orm.Answer.objects.all(): >> > >> >#This Works. >> >blog, is_created = >> > orm['blog.Post'].objects.get_or_create(title=answer_object.answer[: >> > 100]) >> >blog.save() >> > >> >#This DOES NOT work >> >chapter, is_created = >> > orm['journal.Chapter'].objects.get_or_create(content_object=blog) >> >chapter.save() >> >#cleanup task, not relevant to this question below >> >answer_object.chapter_ptr = chapter >> >answer_object.save() >> > >> > >> > >> > But as expected this throws an error on " >> > orm['journal.Chapter'].objects.get_or_create(content_object=blog)" >> > saying that > django.core.exceptions.FieldError: Cannot resolve >> > keyword 'content_object' into field. >> > >> > This is presumably due to content_object being a GenericForeignKey so >> > some operations are not allowed. But I also tried other alternatives >> > for creating the "chapter" object like, >> > >> > chapter = orm['journal.Chapter'](content_object=blog) >> > ERROR > TypeError: 'content_object' is an invalid keyword argument for >> > this function >> > >> > and >> > >> > chapter = orm.journal.Chapter(content_object=blog) >> > ERROR > AttributeError: The model 'journal' from the app 'questions' >> > is not available in this migration. (Did you use orm.ModelName, not >> > orm['app.ModelName']?) >> > >> > So since my earlier approach was failing I tried a new tact. The model >> > whose instantiation was failing in my code above i.e. Chapter in the >> > Journal app, I decided to create a datamigration for that instead. I >> > also made sure to --freeze the models I am referring to in the >> > forwards definition. Now this should have been straight forward, I >> > would think. I have my forward code as follows - >> > >> > def forwards(self, orm): >> > >> >for answer_object in orm['questions.Answer'].objects.all(): >> > >> >#Works, AGAIN! >> >blog, is_created = >> > orm['blog.Post'].objects.get_or_create(title=answer_object.answer[: >> > 100]) >> >blog.save() >> > >> ># DOES NOT WORK, AGAIN! >> >chapter = orm.Chapter(rank=1, cont
Re: Could not parse the remainder: |slice
are you sure you used proper syntax? {{ sliceable|slice:":2" }} https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#slice On Sat, Aug 13, 2011 at 7:28 PM, electrocoder wrote: > Blog on my local PC with "Could not parse the remainder: | Slice" I get the > error message. How can I fix. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/AeqglbCy8ZIJ. > 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. > -- Thanks & Regards, Chintan Tank Software Developer School of Library & Information Science Indiana University, Bloomington. http://www.cs.indiana.edu/~cdtank/ -- 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: Ynt: Re: Could not parse the remainder: |slice
*can you paste the entire error stacktrace? use http://dpaste.de/ or equivalent to share it. * On Sat, Aug 13, 2011 at 8:01 PM, electrocoder wrote: > my syntax is *{% autoescape off %} {{ myobje.mypage|slice:":150" }} {% > endautoescape %}* > * > * > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/UpRrFOATAeIJ. > 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. > -- Thanks & Regards, Chintan Tank Software Developer School of Library & Information Science Indiana University, Bloomington. http://www.cs.indiana.edu/~cdtank/ -- 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.