Django-superform不呈现

时间:2016-08-22 11:49:11

标签: python django

我的规格:
Django 1.9
Python 3.5.1
django-superform 0.3.1

我的目标:
我希望bod_quota形式与CustomerForm

嵌套

我的尝试

我已按照文档https://github.com/gregmuellegger/django-superform进行操作 但它不会呈现嵌套形式。 customer/model.py

class Customer(models.Model):
    customer_code = models.CharField(max_length=10, unique=True)
    name_th = models.CharField(max_length=100)
    name_en = models.CharField(max_length=100)

customer/forms.py

class CustomerForm(SuperModelForm):
    manual_quota = InlineFormSetField(parent_model=Customer, model=BodQuota, fields = (
            "quota_per_occurrence_type",
            "quota_by_month",
            "quota_by_year",
            "quota_count_method_type",
        ) )

    class Meta:
        model = Customer
        fields = [
            'customer_code',
            'name_th',
            'name_en',
        ]

bod_quota/model.py

class BodQuota(models.Model):
    #
    # Change Quota General Types
    #
    class QuotaPerOccurrenceType(DjangoChoices):
        monthly = ChoiceItem('monthly')
        yearly = ChoiceItem('yearly')

    class QuotaCountMethodType(DjangoChoices):
        circuit_based = ChoiceItem('circuit_based')
        customer_based = ChoiceItem('customer_based')

    customer = models.ForeignKey(Customer, default=None)
    quota_per_occurrence_type = models.CharField(
        max_length=10,
        choices=QuotaPerOccurrenceType.choices,
        validators=[QuotaPerOccurrenceType.validator],
        default=QuotaPerOccurrenceType.monthly)
    quota_by_month = models.PositiveSmallIntegerField(
        null=False, blank=False, default=0, help_text=_("monthly quota"))
    quota_by_year = models.PositiveSmallIntegerField(
        null=False, blank=False, default=0, help_text=_("annually quota"))
    quota_count_method_type = models.CharField(
        max_length=20,
        choices=QuotaCountMethodType.choices,
        validators=[QuotaCountMethodType.validator],
        default=QuotaCountMethodType.customer_based)

bod_quota/forms.py

class BodQuotaForm(ModelForm):
    class Meta:
        model = BodQuota
        fields = (
            "quota_per_occurrence_type",
            "quota_by_month",
            "quota_by_year",
            "quota_count_method_type",
        )


BodQuotaFormSet = modelformset_factory(BodQuota, form=BodQuotaForm)

问题: manual_quota未在浏览器中显示

1 个答案:

答案 0 :(得分:0)

有两种方法可以在浏览器中显示。

•   views methods: I think it’s well explained here

http://tutorial.djangogirls.org/en/django_forms/

•   django admin interface

就个人而言,我习惯于以第一种方式从django管理界面开始。

根据你的例子,我可以复制下面的表格。

如果您正在寻找,请检查:

1.你在你的setting.py文件中有这个

INSTALLED_APPS = [
    #TESTING DJANGO SUPER_FORM_PCKG
    'django_superform',
    'customer',
    'djchoices',
    'bod_quota’,
    #DEFAULT DJANGO SETTING   
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

2.在客户应用文件夹下的admin.py文件中,添加以下代码

from django.contrib import admin
from customer.models import *
admin.autodiscover()
class CustomerAdmin(admin.ModelAdmin):
    fields = ('customer_code', 'name_th','name_en')
admin.site.register(Customer, CustomerAdmin)

在界面中,您将看到以下表格

enter image description here

3.在bod_quota app文件夹下的admin.py文件中,添加以下代码

from customer.forms import *
admin.autodiscover()
class BodQuotaAdmin(admin.ModelAdmin):
    fields  = [ 'customer','quota_per_occurrence_type','quota_by_month','quota_by_year','quota_count_method_type']
    form    = CustomerForm
admin.site.register(BodQuota, BodQuotaAdmin)

在界面中,您将看到以下表格

enter image description here

希望这有帮助。