2

問題の解決策としてインライン フォームセットを試すためのヒントが与えられました。サンドボックスプロジェクトで実験中です。Django ドキュメントとまったく同じを複製しようとしていますが、失敗します。:(

モデル:

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)**

意見:

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,    })

管理画面で と を正常に作成しましAuthor1Author2

Book1Book1b割り当てられAuthor1
Book2ますAuthor2

試してみると、<class 'Sandbox_App.models.Book'> has no ForeignKey to <class 'Sandbox_App.models.Author'>

トレースバック:

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'>

なぜこれが機能しないのですか?

どうもありがとう

4

1 に答える 1

1

エラー メッセージの状態のように、Book には Author への Foreignkey が必要です。ManyToManyRelation を使用しています。

authors = models.ManyToManyField(Author)

シェルのドキュメントの例をテストしましたが、動作します。

こちらもご覧ください: Django inlineformset_factory および ManyToMany フィールド

于 2012-08-15T21:17:27.233 に答える