Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6319379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:47:50+00:00 2026-05-24T15:47:50+00:00

I need to perform a datamigration of a model Answer in app Question .

  • 0

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 where am I going wrong? Any pointers appreciated. Thanks.

UPDATE

So since my earlier approach was failing I tried a new tack. 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()

UPDATE 2
Wanted to add that 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:47:50+00:00Added an answer on May 24, 2026 at 3:47 pm

    After getting help from Rob and folks on the South & Django user groups I was able to resolve this issue. Below is the definition of my forwards datamigration script.

    def forwards(self, orm):
    
        for answer_object in orm['questions.Answer'].objects.all():
    
    
            blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[:100])
            blog.save()
    
            #I have to manually lookup the content_type ans set it in the chapter creation.
            ct = orm['contenttypes.ContentType'].objects.get(app_label="blog", model="post")    
            chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id)
    
            chapter.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LinkButton that I need to perform a click on to cause
I need to perform my script (a search) on all the files of a
I need to perform a range of simple calculations on an Invoice model object,
I need to perform a bitwise equality between two bytes. That means that for
I need to perform a group by, but only on rows that meet a
I need to perform several action on a html file such as removing a
I need to perform queries in a ADO.NET model by using LINQ. I have
I need to perform some action when my application receives focus. I've tried hooking
I need to perform Diffs between Java strings. I would like to be able
I need to perform a filtered query from within a django template, to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.