2012-08-15 68 views
2

我已经给出了一个提示,试用内联formset作为问题的解决方案。 只是在沙箱项目中进行实验。我试图复制与Django文档中完全相同的example,并且它失败。 :(Django:文档中的内嵌formset示例

型号:

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
Book2Author2

当我尝试它,我得到一个<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'> 

这是为什么不工作?

非常感谢

+0

你可以发布一个完整的追溯? – 2012-08-15 20:33:55

+0

感谢Burhan。请在这里查看完整的回溯:http://www.heypasteit.com/clip/0FWM – Houman 2012-08-15 20:52:56

回答

1

像错误消息状态,本书应该有一个外键来创作。您正在使用ManyToManyRelation。

authors = models.ManyToManyField(Author) 

我测试了shell中docs的例子,它的工作原理。

也看这里:Django inlineformset_factory and ManyToMany fields

+0

谢谢金戈,我现在看到,这是如何工作的。有趣的是,你和我正在为我们的表单寻找类似的解决方案。所以这个内联formset不可能是所有的答案... – Houman 2012-08-15 21:35:09

+1

你是对的...... :) – Jingo 2012-08-15 21:37:34