2014-01-26 32 views
3

建立了一個表格,對於我在定義的兩個模型一個小應用程序的最佳方式:什麼是從2種機型的Django 1.6

class Pressure (models.Model): 
    user = models.ForeignKey(User) 
    sys = models.FloatField(blank=False, null=False, verbose_name=_(u"systolic")) 
    dia = models.FloatField(blank=False, null=False, verbose_name=_(u"diastolic")) 
    pulse = models.IntegerField(blank=False, null=False, verbose_name=_(u"Pulse")) 
    timestamp = models.DateTimeField(verbose_name=_(u"Messurement timestamp")) 

    def __unicode__(self): 
     return self.timestamp.strftime("%y-%m-%d: %H-%M") 

class Weight(models.Model): 
    user = models.ForeignKey(User) 
    weight = models.FloatField(blank=False, null=False, verbose_name=_(u"Weight")) 
    timestamp = models.DateTimeField(verbose_name=_(u"Messurement timestamp")) 

    def __unicode__(self): 
     return self.timestamp.strftime("%y-%m-%d: %H-%M") 

不,我想建立一個合併的形式都這個地方的兩個型號的用戶和時間戳都是一樣的。其餘的文件應該是可選的。這意味着,如果沒有給出例如重量,不應該插入到數據庫等。

我正在閱讀有關mixins和一些formset,但落後一個好主意這裏最好的辦法是什麼。

回答

1

你並不需要一個單一的形式來處理這個問題。您只需要一個處理兩個模型的表單的視圖。基本思想很簡單:創建兩個表單類,並將這兩個表單呈現給在同一個<form>元素中呈現的用戶。提交時,請檢查兩個表單是否有效,以及何時將它們保存並將用戶發送到下一頁。它看起來像這樣:

from django import forms 
from django.contrib.auth.decorators import login_required 
from django.shortcuts import render, redirect 
from django.utils.timezone import now 

from .models import Pressure, Weight 


class PressureForm(forms.ModelForm): 

    class Meta: 
     model = Pressure 
     exclude = ('user', 'timestamp',) 


class WeightForm(forms.ModelForm): 

    class Meta: 
     model = Weight 
     exclude = ('user', 'timestamp',) 


@login_required 
def submit(request): 
    if request.method == 'POST' 
     pressure_form = PressureForm(request.POST) 
     weight_form = WeightForm(request.POST) 
     if all(form.is_valid() for form in [pressure_form, weight_form]): 
      record_time = now() 
      pressure = pressure_form.save(commit=False) 
      pressure.timestamp = record_time 
      pressure.user = request.user 
      pressure.save() 
      weight = weight_form.save(commit=False) 
      weight.timestamp = record_time 
      weight.user = request.user 
      weight.save() 
      return redirect('<insert-name-of-success-url>') 
    else: 
     pressure_form = PressureForm() 
     weight_form = WeightForm() 
    context = { 
     'pressure_form': pressure_form, 
     'weight_form': weight_form, 
    } 
    return render(request, '<insert-template-name>', context) 

你不需要formset或通用視圖來處理這個問題。這是一個簡單而直接的觀點。如果你發現自己經常重複這個模式,那麼你可能會創建一些視圖類來處理這個問題。內置的仿製藥都不會爲您做到這一點。

+0

在POST時接收''WeightForm'對象不可調用'。現在還沒有想法 – frlan

+0

保存'weight_form'缺少對'save'的實際調用。我做了編輯。 –

+0

葉普,一旦你指出它很清楚....它的工作。謝謝。 – frlan

1

您可以爲您的2個模型創建2個ModelFrom。並且在其中一個表單中排除'user'和'timestamp'字段。在模板中將兩個表單中的字段放在一個<form>標記中。鑑於你應該先救與「用戶」和「時間戳」的形式,然後把數據從它實例與第二種形式創建:

class PressureForm(forms.ModelForm): 
    class Meta: 
     model = Pressure 

class WeightForm(forms.ModelForm): 
    class Meta: 
     model = Weight 
     exclude = ('user', 'timestamp') 

def someview(request): 
    if request.method == 'POST': 
     pressure_form = PressureForm(request.POST) # user and timestamp is here 
     weight_form = WeightForm(request.POST) 

     if pressure_form.is_valid(): 
      pressure = pressure_form.save() 

      if weight_form.is_valid(): 
       weight = weight_form.save(commit=False) 
       weight.user = pressure.user 
       weight.timestamp = pressure.timestamp 
       weight.save() 
+0

您是否有機會將第二部分轉化爲基於類的視圖? – frlan

+0

@frlan最簡單的方法是標準函數視圖... – ndpu

相關問題