通过我的django模型

时间:2017-09-20 21:04:37

标签: python django

settings.py

PERIODS = (
    ('day', _('Per day')),
    ('week', _('Per week')),
    ('month', _('Per month')),
    ('year', _('Per year')), 
) 

models.py

class Statistics(TimeStampedModel):

    @property
    def per_period(self):
        return settings.PERIODS[self.] 

    def nb_of_customers_per_period(self):
        pass

views.py

class StatisticsIndexView(StaffRestrictedMixin, TemplateView):
    model = Statistics()
    template_name = 'loanwolf/statistics/index.html'

    def get_context_data(self, **kwargs):
        context = super(StatisticsIndexView, self).get_context_data(**kwargs)
        context.update({
            'applications_by_state': ApplicationsByState(),
            'applications_calendar': ApplicationsCalendar(),
            'new_customers_calendar': NewCustomersCalendar(),
            'statistics': Statistics(),
            'form': StatisticsBaseForm(),
        })
        return context

forms.py

class StatisticsBaseForm(forms.Form):
    type_choice = forms.ChoiceField(label=_("Type"), choices=settings.STATISTICS_TYPE_CHOICES, initial=0, required=False)
    period = forms.ChoiceField(label="Period", choices=settings.PERIODS, initial='week', required=False)
    from_regular_product = forms.ModelChoiceField(
        queryset=ProductConfig.objects.filter(pk=-1), required=False,
        label=_('Product'))
    from_special_product = forms.ModelChoiceField(
        queryset=ProductConfig.objects.filter(pk=-1), required=False,
        label=_('Product'))
    product_type = forms.ChoiceField(
        choices=settings.LOANWOLF_PRODUCT_TYPE_CHOICES, required=False,
        initial='regular', label=_('Product type'))
    debit_frequency = forms.ChoiceField(
        choices=settings.LOANWOLF_PRODUCT_DEBIT_FREQUENCIES_CHOICES,
        required=False)

    def __init__(self, *args, **kwargs):
        super(StatisticsBaseForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_class = 'row'
        self.helper.layout = StatisticalToolsLayout

        company = get_current_company()
        regular_products = company.available_products.filter(
            is_active=True, product_type='regular')
        special_products = company.available_products.filter(
            is_active=True, product_type='special')

        self.fields['from_regular_product'].queryset = regular_products
        self.fields['from_special_product'].queryset = special_products

        if regular_products:
            self.fields['from_regular_product'].initial = \
                settings.LOANWOLF_EXTERNAL_REQUEST_DEFAULT_PRODUCT_INDEX

        if special_products:
            self.fields['from_special_product'].initial = \
                settings.LOANWOLF_EXTERNAL_REQUEST_DEFAULT_PRODUCT_INDEX

    class Meta:
        model = Statistics
        fields = '__all__'

好的,这是我的问题。我有一个form,其中包含period下拉菜单,可让我选择不同类型的句点(Per dayPer weekPer month或{{1 }})。例如,如果我选择Per year,我希望通过Per week模型使用self.something访问此选项。对不起,但我是Django / Python的新程序员。我怎么能这样做?我是否需要使用form中的Statistics按钮发送内容?

更新

在我的django项目中,可以使用Apply向应用程序中的每个客户显示并找到特定客户的创建日期

CustomerProfile.objects.all()

根据创建日期,我想列出每天,每周,每月或每年创建的客户数量。结果的一个例子可能是

In [12]: cust = CustomerProfile.objects.get(pk=100)

In [13]: cust.user.date_joined
Out[13]: datetime.datetime(2017, 7, 28, 14, 43, 51, 925548)

In [14]: cust
Out[14]: <CustomerProfile: FistName LastName's customer profile>

我可能需要一个范围start_date和end_date,我们将列出那种信息。 start_date将是第一个客户创建的日期,第一周创建将是第一个日期的一周。显然,end_date是今天,而最后一周是这个end_date的一周。

例如,如果我在下拉菜单中选择... week 28 : [ 09/07/2017 - 15/07/2017 ] - Count : 201 customers ... 并在表单中按Per week,我想向我的模型发送信息,我可以编码我解释的内容。

1 个答案:

答案 0 :(得分:1)

我认为您可以在表单中添加save()方法:

class StatisticsBaseForm(forms.Form):
    ... # the rest of the code
    def save(self):
        period = self.cleaned_data['period']
        # do something with the period
        get_user_create_by_period(period)

并在您的观看中添加此表单,如下所示:

class StatisticsIndexView(StaffRestrictedMixin, TemplateView):
    model = Statistics
    template_name = 'loanwolf/statistics/index.html'
    form_class = StatisticsBaseForm

当视图检查所有提交的数据是否正常时,将调用表单的save方法。在表格的save()方法中,您可以访问提交的数据,例如self.cleaned_data['period']

我实际上甚至认为统计模型在这里没用。