&#34; <关联:>&#34;需要有一个字段&#34;关联&#34;在此之前可以使用多对多关系:django错误

时间:2017-06-17 15:19:15

标签: django

我在SO中看到了一些类似的线程,但似乎无法解决它。这是我的模特:

class Association(models.Model):
'''
For members declared as groups
'''
member=models.ManyToManyField(User,related_name='association_user')
name=models.CharField(max_length=50)


def __str__(self):
    return self.name

class Meta:
    ordering=('name',)

多对多关系最终创建了一个引人注目的表格,其中包含以下定义:

association_id
user_id

我不知道它是否引用该字段关联,也不知道如何从我的视图中向该表添加值。

my forms.py

class NewAssocationForm(forms.ModelForm):
'''
this is used to create a new form



'''
class Meta:
    model=Association
    fields=['name',]
    labels=['Assocation Name'),]

和我的views.py:

@login_required
def make_new(request):
'''
Register a new association or corporate
'''

 if request.method=='POST':

    form=NewAssocationForm(request.POST or None)

        if form.is_valid():

            new_association=form.save(commit=False)
            new_association.member=request.user


            new_association.save()


            #add a message then redirect

            return HttpResponseRedirect('/corporates/done/)

        else:
            form=NewAssocationForm(request.POST or None)



    else:
        form=NewAssocationForm(request.POST or None)

 return render(request, "association_new.html", locals())

我的模型中根本没有名为关联的字段。我怀疑它是约束因为我可以从shell向表中插入记录而没有任何问题。

这是我的追溯。

Environment:


Request Method: POST
Request URL: http://localhost:9000/corporates/new/

Django Version: 1.10
Python Version: 3.4.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'widget_tweaks',
 'welcome',
 'members',
 'corporates']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "D:\pyworks\agrigo\env\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "D:\pyworks\agrigo\env\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "D:\pyworks\agrigo\env\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\pyworks\agrigo\env\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "D:\pyworks\agrigo\env\agrigo\corporates\views.py" in make_new
  78.               new_association.member=request.user

File "D:\pyworks\agrigo\env\lib\site-packages\django\db\models\fields\related_descriptors.py" in __set__
  499.         manager = self.__get__(instance)

File "D:\pyworks\agrigo\env\lib\site-packages\django\db\models\fields\related_descriptors.py" in __get__
  476.         return self.related_manager_cls(instance)

File "D:\pyworks\agrigo\env\lib\site-packages\django\db\models\fields\related_descriptors.py" in __init__
  783.                                  (instance, self.source_field_name))

Exception Type: ValueError at /corporates/new/
Exception Value: "<Association: North Side>" needs to have a value for field "association" before this many-to-many relationship can be used.

1 个答案:

答案 0 :(得分:0)

感谢所有经历过长篇文章的人。显然,我不应该指定用户。所以,删除这一行:

new_association.member=request.user

解决了这个问题。说得通;愚蠢的我。