2017-07-24 122 views
1

我的應用程序是項目管理工具,用戶可以在其中添加,編輯和查看項目。項目有標題,摘要和作者(用戶)。用戶與項目具有ManyToMany關係。Django ModelForm中的一個字段(ManyToMany)未保存到數據庫

添加新項目工作正常,直到我添加了一個編輯項目視圖。我仍然可以創建一個新項目或編輯現有項目,並將新標題和摘要保存到數據庫中,但所選作者不會保存。請注意,我仍然可以進入shell並手動將作者添加到項目中。

這裏是項目用戶模式

class MyUser(AbstractBaseUser): 
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) 
    bank_id = models.CharField("researcher's four-letter bank id", null=True, max_length=4, unique=True) 
    #division = models.CharField(max_length=30, blank = True) 
    department = models.CharField(max_length=3, choices=DEPARTMENTS) 
    job_title = models.CharField("job title", max_length=30, choices=JOB_TITLES) 
    citations = models.IntegerField(null=True, blank=True) 
    institution = models.CharField(max_length=30, choices=DEPARTMENTS, blank=True) 
    created_on = models.DateTimeField(auto_now_add=True) 
    updated_on = models.DateTimeField(auto_now=True) 
    #history = HistoricalRecords() 
    REQUIRED_FIELDS = ['email'] 
    USERNAME_FIELD = 'username' 

class Project(models.Model): 
    title = models.TextField('Title') 
    summary = models.TextField('Summary', default=DEFAULT_TEXT) 
    authors = models.ManyToManyField(MyUser) 
    internal_status = models.CharField('Internal Status', max_length = 20, choices = INTERNAL_STATUS, 
             default='wip') 
    external_status = models.CharField('External Status', max_length = 20, choices = EXTERNAL_STATUS, 
             blank=True) 
    mtp_goal = models.CharField(max_length = 50, choices = MTP_GOALS, 
           blank=True) 
    topics = ArrayField(models.CharField('Topics', max_length=30), size=4, null=True) 
    created_on = models.DateTimeField(auto_now_add=True, null=True) 
    updated_on = models.DateTimeField(auto_now=True, null=True) 
    history = HistoricalRecords() 

views.py

def add_new(request): 
    if request.method == 'POST': 
     form = ProjectForm(request.POST) 
     if form.is_valid(): 
      project = form.save(commit=False) 
      project.created_on = timezone.now() 
      project.save() 
      return redirect('project_profile', pk=project.pk) 
    else: 
     form = ProjectForm() 
    return render(request, 'add_new.html', {'form': form}) 

def edit_project(request, pk): 
    project = get_object_or_404(Project, pk=pk) 
    if request.method == 'POST': 
     form = ProjectForm(request.POST, instance=project) 
     if form.is_valid(): 
      project = form.save(commit=False) 
      project.updated_on = timezone.now() 
      project.save() 
      return redirect('project_profile', pk=project.pk) 
    else: 
     form = ProjectForm(instance=project) 
    return render(request, 'edit_project.html', {'form': form}) 

forms.py:

from django import forms 
from .models import Project, MyUser 

class ProjectForm(forms.ModelForm): 

    class Meta: 
     model = Project 
     fields = ('title', 'authors', 'summary') 

最後,add_project.html頁面(請原諒可怕的HTML):

<html> 
<head> 
    <title>ResearchTracker</title> 
</head> 
<body> 
    <div> 
    <nav> 
     <a href="/about">About us</a> 
     <a href="/login">Login</a> 
     <a href="/admin">Admin</a> 
    </nav> 
    </div> 
     <h1>New project</h1><br> 
    <form method="POST" class="post-form">{% csrf_token %} 
     {{ form.as_p }} 
     <button type="submit" class="save btn btn-default">Save</button> 
    </form> 
    <a href="/">Go home</a> 
    </body> 
</html> 

回答

3

由於您使用的savecommit=False,你需要顯式調用save_m2m到許多保存到很多領域。

the documentation

調用save_m2m()如果您使用保存時,才需要提交(commit = FALSE)。 當您在窗體上使用簡單的保存()時,所有數據(包括多對多數據)都會保存,而不需要任何其他方法調用 。例如:

你會做

project.save() 
project.save_m2m() 
+0

嘿它的工作,非常感謝!順便說一句,第二步實際上是'form.save_m2m()' – JTa

相關問題