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 8392255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:28:18+00:00 2026-06-09T19:28:18+00:00

I have been given a hint to try out the inline formset as a

  • 0

I have been given a hint to try out the inline formset as a solution to a problem.
Just experimenting with it in a sandbox project. I am trying to replicate the exact same example as in the Django documentation and it fails. 🙁

Models:

TITLE_CHOICES = (
    ('MR', 'Mr.'),
    ('MRS', 'Mrs.'),
    ('MS', 'Ms.'),
) 

class Author(models.Model):
    name = models.CharField(max_length=100)
    title = models.CharField(max_length=3, choices=TITLE_CHOICES)
    birth_date = models.DateField(blank=True, null=True)

    def __unicode__(self):
        return self.name

class Book(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)**

View:

def manage_books(request, author_id):
    author = Author.objects.get(pk=author_id)
    BookInlineFormSet = inlineformset_factory(Author, Book)
    if request.method == "POST":
        formset = BookInlineFormSet(request.POST, request.FILES, instance=author)
        if formset.is_valid():
            formset.save()
            # Do something. Should generally end with a redirect. For example:
            return HttpResponseRedirect(author.get_absolute_url())
    else:
        formset = BookInlineFormSet(instance=author)
    return render_to_response("manage_books.html", {        "formset": formset,    })

In the admin screen I have successfully created an Author1 and Author2.

Book1 and Book1b are assigned to Author1
Book2 to Author2.

When I try it I get a <class 'Sandbox_App.models.Book'> has no ForeignKey to <class 'Sandbox_App.models.Author'>

Traceback:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/test/1

Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',a
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Sandbox_App',
 'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/houman/projects/Sandbox/Sandbox_App/views.py" in manage_books
  58.     BookInlineFormSet = inlineformset_factory(Author, Book)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in inlineformset_factory
  817.     fk = _get_foreign_key(parent_model, model, fk_name=fk_name)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _get_foreign_key
  800.             raise Exception("%s has no ForeignKey to %s" % (model, parent_model))

Exception Type: Exception at /test/1
Exception Value: <class 'Sandbox_App.models.Book'> has no ForeignKey to <class 'Sandbox_App.models.Author'>

Why is this not working?

Many Thanks

  • 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-06-09T19:28:20+00:00Added an answer on June 9, 2026 at 7:28 pm

    Like the error message states, Book should have a Foreignkey to Author. You are using a ManyToManyRelation.

    authors = models.ManyToManyField(Author)
    

    I tested the example from the docs in the shell and it works.

    Also look here: Django inlineformset_factory and ManyToMany fields

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been given some poorly formatted data and need to pull numbers out
I have been given a problem to decrypt this text: 3e47b75000b0924b6c9ba5759a7cf15d which is supposedly
I have been given control of a web site in Classic ASP. What is
I have been given an exercise to solve the zebra puzzle using a constraint
I have been given the below .NET question in an interview. I don’t know
We have been given a site xyz.com, in which the home page has images
I have been given the following request. Please give 7% of the current contacts
I have been given the option to either have a Windows laptop or a
I have been given the task of adding functionality to an existing IIS 6.0
I have been given the unenviable task of cleaning up after a developer who

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.