开发者

Showing form elements based on another tables data

I have the following models:

class Product(models.Model):
    active = models.BooleanField(default=True)
    name = models.CharField(max_length=40, unique=True)
    acronym = models.CharField(max_length=3, unique=True)
    bool1_name = models.CharField(max_length=60, blank=True, null=True)
    bool1_default = models.BooleanField(default=False)
    int1_name = models.CharField(max_length=60, blank=True, null=True)
    int1_default = models.IntegerField(blank=True, null=True)
    float1_name = models.CharField(max_length=60, blank=True, null=True)
    float1_default = models.FloatField(blank=True, null=True)
    date1_name = models.CharField(max_length=60, blank=True, null=True)

class ProductData(models.Model):
    created = models.DateTimeField(default=datetime.now)
    created_by = models.ForeignKey(User)
    item = models.ManyToManyField(Item)
    bool1_val = models.BooleanField(default=False)
    int1_val = models.IntegerField(blank=True, null=True)
    float1_val = models.FloatField(blank=True, null=True)
    date1_val = models.DateField(blank=True, null=True)

class Item(models.Model):
    product = models.ForeignKey(Product)
    business = models.ForeignKey(Business)

And I have put in the following data into the database:

# pseudo code

Product(1,'Toothpaste','TP','Is the toothpaste white?',1,,,'Weight',1,)
Product(1,'Milk','MLK',,,,,'Litres',2,'Best Before')

I want to be able to build a form for ProductData based on the variables defined in Product (create-a-form-based-on-the-values-from-another-table). I want something like this:

class ProductDataForm(ModelForm):

    def __init__(self,p,*args,**kwargs):
        super(ProductDataForm, self).__init__(*args, **kwargs)
        # if the values isn't set in the product
        if p.bool1_name is None:
            # hide the input
            self.fields['bool1_val'].widget = forms.CharField(required=False)
        else:
            # else make sure the name the field name is the product name
            self.fields['bool1_val'].widget = forms.BooleanField(label=p.bool1_name)

...

But I'm having a problem passing an instance of Product to ProductDataForm. Others have said I could use a BaseModelFormSet but literature on this is sketchy and I'm not to sure how to apply it.

EDIT

If I create an array of all the fields I DON'T want to show in ProductDataForm's init how can I pass these to the Meta class's exclude. Like so:

class ProductDataForm(ModelForm):

    def __init__(self,p,*args,**kwargs):
        super(ProductDataForm, self).__init__(*args, **kwargs)
        tempExclude = []
        if not p.bool1_name:
            tempExclude.append('bool1_val')
        else:
            self.fields['bool1_val'].label = p.bool1_name

        self.Meta.exclude = tempExclude

    class Meta:
        model = ProductData
        exclude = []

EDIT

I'm now trying to store the fields I want to 开发者_开发百科exclude in the setting.py file like so:

# settings.py
SUBITEM_EXCLUDE_FIELDS = ['dave']

# views.py
def new_product_data_view(request,product='0'):
    try:
        i_fpKEY = int(product)
    except ValueError:
        raise Http404()
    if not i_fpKEY:
        t_fp = Product.objects.filter(active=1).order_by('id')[0]
    else:
        t_fp = Product.objects.get(id=i_fpKEY)
    FieldsToExcludeFromProductDataForm(t_fp)
    print "views.py > PRODUCTDATA_EXCLUDE_FIELDS = "+str(PRODUCTDATA_EXCLUDE_FIELDS)
    siForm = ProductDataForm(t_fp, request.POST, auto_id='si_%s')
    return render_to_response(...)

# middleware.py
def FieldsToExcludeFromProductDataForm(tempFP):
    excludedFields = ['created','created_by','item']
    if not tempFP.bool1_name:
        excludedFields.append('bool1_val')
    if not tempFP.int1_name:
        excludedFields.append('int1_val')
    ...
    for val in excludedFields:
        PRODUCTDATA_EXCLUDE_FIELDS.append(val)
    print "middleware.py > PRODUCTDATA_EXCLUDE_FIELDS = "+str(PRODUCTDATA_EXCLUDE_FIELDS)

# forms.py
class ProductDataForm(ModelForm):

    # Only renames the fields based on whether the product has a name
    # for the field. The exclusion list is made in middleware
    def __init__(self,fp,*args,**kwargs):
        super(ProductDataForm, self).__init__(*args, **kwargs)
        if fp.bool1_name:
            self.fields['bool1_val'].label = fp.bool1_name
        if fp.int1_name:
            self.fields['int1_val'].label = fp.int1_name

    class Meta:

        def __init__(self,*args,**kwargs):
            super(Meta, self).__init__(*args, **kwargs)
            print 'Meta > __init__ > PRODUCTDATA_EXCLUDE_FIELDS = '+str(PRODUCTDATA_EXCLUDE_FIELDS)

        model = ProductData
        print 'Meta > PRODUCTDATA_EXCLUDE_FIELDS = '+str(PRODUCTDATA_EXCLUDE_FIELDS)
        #exclude = PRODUCTDATA_EXCLUDE_FIELDS

But terminal shows that the Meta class gets processed very early on and therefore can't get the newly amended PRODUCTDATA_EXCLUDE_FIELDS :

Meta > PRODUCTDATA_EXCLUDE_FIELDS = ['dave']
[11/Jul/2011 15:51:31] "GET /page/profile/1/ HTTP/1.1" 200 11410
middleware.py > PRODUCTDATA_EXCLUDE_FIELDS = ['dave', 'created', 'created_by', 'item', 'bool1_val', 'int1_val']
views.py > PRODUCTDATA_EXCLUDE_FIELDS = ['dave', 'created', 'created_by', 'item', 'bool1_val', 'int1_val']
[11/Jul/2011 15:51:32] "GET /item/new/ HTTP/1.1" 200 5028
[11/Jul/2011 15:51:32] "GET /client/1/ HTTP/1.1" 200 5445
[11/Jul/2011 15:51:32] "GET /client/view/1/ HTTP/1.1" 200 3082


Why bother with exclude, and metaclasses - just delete fields you want to exclude:

class ProductDataForm(ModelForm):
     __init__():
         ...
          for field_name in PRODUCTDATA_EXCLUDE_FIELDS:
              del self.fields[field_name]
         ...

Maybe it's not quite right, but it's simple, and it works.


Your idea was spot on but then you tried to implement it in a very roundabout way. Python lets you create classes dynamically at runtime using the three argument form of the type() function. The common way to make use of this is with a factory function and Django provides just that in the form of django.forms.models.modelform_factory. This function isn't documented but that seem to be in progress. It's from the same family of functions as modelformset_factory and inlineformset_factory which are documented so I'd say it's safe to use.

The signature is

modelform_factory(model, form=ModelForm, fields=None, exclude=None, formfield_callback=None)

model, exclude and fields are equivalent to what you would normally declare in the form's inner Meta class (and that's how it's implemented).

Now, to use this in your example you'll want to change your approach a bit. First import the factory function.

from django.forms.models import modelformset_factory

Then use your view function or class to:

  1. Get the product instance.
  2. Generate a list of excluded fields based on the product instance (I'll call this excludedFields).
  3. Create a form class:

    formClass = modelform_factory(ProductData, excludes=excludedFields)

  4. Initialise your form as usual, but using formClass instead of a predefined form (ProductDataForm)

    form = formClass() or form = formClass(request.POST) etc...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜