2017-05-09 79 views
0

我有1種形式的2個模型。在這種形式下數據可以改變。 對於1模型(Sloep_core),內容只需要更新,這是行得通的。 其他模型(Sloep_gegevens)的內容通常會更新。 只有在模型'Sloep_gegevens'的形式中更改'sloepnaam'值時,模型'Sloep_gegevens'的記錄必須輸入新記錄,而不更新舊記錄。 save()必須從update更改爲insert,但是如何?Django多模式表單,在一個模型上保存或更新

我試圖把soms代碼放在一起(用一些搜索操作)。我來到下面的代碼,但現在它總是插入代碼。有人有一個想法讓它正確嗎?

model.py:

class Sloep_core(models.Model): 
     FSN_nummer = models.IntegerField(unique=True) 
     HT_nummer = models.IntegerField(unique=True, blank=True, null=True) 
     SRNL_nummer = models.IntegerField(blank=True, null=True) 
     sloep_type = models.CharField(max_length=128, blank=True) 
     werf = models.CharField(max_length=255, blank=True) 
     info = models.TextField(blank=True) 
     objects = SloepManager() 

     def __str__(self): 
       return str(self.FSN_nummer) 

class Sloep_gegevens(models.Model): 
     sloep = models.ForeignKey(Sloep_core, on_delete=models.CASCADE, default='') 
     sloepnaam = models.CharField(max_length=128) 
     thuishaven = models.CharField(max_length=128, blank=True) 
     date = models.DateField(auto_now=True) 
     __original_sloepnaam = None 

     def __init__(self, *args, **kwargs): 
       super(Sloep_gegevens, self).__init__(*args, **kwargs) 
       self.__original_sloepnaam = self.sloepnaam 

     def save(self, force_insert=False, force_update=False, *args, **kwargs): 
       if self.sloepnaam != self.__original_sloepnaam: 
         # then do this 
         force_insert = True 
       else: 
         # do that 
         force_update = True 

       super(Sloep_gegevens, self).save(force_insert, force_update, *args, **kwargs) 
       self.__original_sloepnaam = self.sloepnaam 

     class Meta: 
       ordering = ["date"] 

     def __str__(self): 
       return self.sloepnaam 

form.py:

class SloepGegevensForm(forms.ModelForm): 
     class Meta: 
       model = Sloep_gegevens 
       exclude = ['pk', 'sloep'] 

class SloepCoreForm(forms.ModelForm):   
     class Meta: 
       model = Sloep_core 
       exclude = ['pk', 'SRNL_nummer'] 

views.py:

def sloep_edit(request, pk): 
    sloep = Sloep_core.objects.get(pk=pk) 
    sloep_sg = Sloep_gegevens.objects.filter(sloep=pk).order_by('-date')[0] 
    if request.method == "POST": 
     formSG = SloepGegevensForm(request.POST) 
     formSC = SloepCoreForm(request.POST, instance=sloep) 
     if all([formSG.is_valid(), formSC.is_valid()]): 
      sloep = formSC.save() 
      SG = formSG.save(commit=False) 
      SG.sloep = sloep 
      SG.save() 
      return redirect('sloep_detail', pk=sloep.pk) 
    else: 
     formSG = SloepGegevensForm(instance=sloep_sg) 
     formSC = SloepCoreForm(instance=sloep) 
    return render(
     request, 
     'sloepen/sloep_edit.html', 
     {'formSG': formSG, 'formSC': formSC,}) 
+0

[檢索已保存的實例時ORM調用init是否可能重複?](http://stackoverflow.com/questions/37175724/does-the-orm-call-init-when-retrieving-a-saved-實例) – e4c5

+0

我看不到它在這個鏈接的重複位置。 – Jeebeevee

回答

0

,我發現我的問題的答案。在視圖中,我沒有在POST request.model中提供SlopGegevensForm的pk。 我改變了:formSG = SloepGegevensForm(request.POST)改成formSG = SloepGegevensForm(request.POST, instance=sloep_sg)
這使save()工作。