2016-05-30 64 views
0

我想在另一個表格中顯示錶格。在另一個表格中顯示錶格

例如,我有這些模型:

class Measure(models.Model): 

    length = models.ForeignKey(Statistics, related_name='Length', null=True, blank=True) 
    surface_area = models.ForeignKey(Statistics, related_name='Surface area+', null=True, blank=True) 
    section_area = models.ForeignKey(Statistics, related_name='Section area+', null=True, blank=True) 
    volume = models.ForeignKey(Statistics, related_name='Volume', null=True, blank=True) 
    diameter = models.ForeignKey(Statistics, related_name='Diameter', null=True, blank=True) 


class Statistics(models.Model): 
    total = models.FloatField('Total', null=True, blank=True) 
    avg = models.FloatField('Average', null=True, blank=True) 
    min = models.FloatField('Minimum', null=True, blank=True) 
    max = models.FloatField('Maximum', null=True, blank=True) 
    standard_deviation = models.FloatField('Standard deviation', null=True, blank=True) 

,然後我有這些形式與之前的機型:

class StatisticsForm(forms.ModelForm): 
    class Meta: 
     model = Statistics 
     fields = '__all__' 

class MeasureForm(forms.ModelForm): 
    class Meta: 
     model = Measure 
     fields = '__all__' 

     # Here I have to say that the right form for each field is the StatisticForm 

表單中的ForeignKey的呈現爲一個組合框包括所有在另一個表中的對象(在我的情況下,統計表),我想用StatsForm的對象替換組合框,所以我可以控制我呈現統計對象的方式

非常感謝。

+0

這不是很清楚你在問什麼,但它聽起來像你正在尋找[formsets](https://docs.djangoproject.com/en/dev/topics/forms/formsets/) – solarissmoke

+0

The ForeignKey in the窗體呈現爲一個組合框包括在另一個表中的所有對象(在我的情況下,統計表),我想用統計窗體的對象替換組合框,所以我可以控制我呈現統計對象的方式 –

回答

2

您的數據庫方案和模型的設計不正確,以解決手頭的問題。你在錯誤的方向上定義了「有很多」的關係。一個Measurement應該有幾個Statistics,但是一個Statistics不應該有很多Measurement

由於您的模型現在正在建立,ForeignKey在關係的錯誤一面。你應該這樣做,而不是:

class Measure(models.Model): 
    @property 
    def length(self): 
     return self.statistics_set.get(name='length') 
    @property 
    def section(self): 
     return self.statistics_set.get(name='section') 
    @property 
    def surface(self): 
     return self.statistics_set.get(name='surface') 
    @property 
    def volume(self): 
     return self.statistics_set.get(name='volume') 
    @property 
    def diameter(self): 
     return self.statistics_set.get(name='diameter') 

class Statistics(models.Model): 
    name = models.CharField(max_length=10) 
    measurement = models.ForeignKey('Measurement') 
    total = models.FloatField('Total', null=True, blank=True) 
    avg = models.FloatField('Average', null=True, blank=True) 
    min = models.FloatField('Minimum', null=True, blank=True) 
    max = models.FloatField('Maximum', null=True, blank=True) 
    standard_deviation = models.FloatField('Standard deviation', null=True, blank=True) 

一旦你解決:

class Measure(models.Model): 
    def save(self,*args,**kwargs): 
     result = super(Measure, self).save(*args,**kwargs) 
     Statistics.objects.create(name='length', measurement=self) 
     Statistics.objects.create(name='section', measurement=self) 
     Statistics.objects.create(name='surface', measurement=self) 
     Statistics.objects.create(name='volume', measurement=self) 
     Statistics.objects.create(name='diameter', measurement=self) 
     return result 

要訪問Statistics S代表一個Measurement在當前的代碼,你可以添加@property快捷鍵一對夫婦提供相同的舒適性物體之間的關係問題變得更容易解決。而不是在形式ForeignKeyFieldsStatistics成爲適當的相關對象,這是由Django常規處理。

由於@solarisssmoke在評論中提到您正在尋找formset。下面是從django documentation展示如何實現你所需要的一個例子:

有問題的型號:

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 __str__(self):    # __unicode__ on Python 2 
     return self.name 

class Book(models.Model): 
    name = models.CharField(max_length=100) 
    authors = models.ManyToManyField(Author) 

而且使用inlineformset_factory,創建所需的表單集視圖:

from django.forms import inlineformset_factory 


def manage_books(request, author_id): 
    author = Author.objects.get(pk=author_id) 
    BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',)) 
    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(request, 'manage_books.html', {'formset': formset}) 

如果性能成爲一個問題也可以看看prefetch_related以提升性能。