Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6151739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:48:41+00:00 2026-05-23T19:48:41+00:00

I have the following models: class Product(models.Model): active = models.BooleanField(default=True) name = models.CharField(max_length=40, unique=True)

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:48:42+00:00Added an answer on May 23, 2026 at 7:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
I have the following models: class Indicator(models.Model): name = models.CharField(max_length=200) category = models.ForeignKey(IndicatorCategory) weight
I have the following model structure below: class Master(models.Model): name = models.CharField(max_length=50) mounting_height =
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
Let's say I have the following model: class Contest: title = models.CharField( max_length =
Say I have the following in my models.py : class Company(models.Model): name = ...
I have the following models: class Product < ActiveRecord::Base belongs_to :brand belongs_to :model accepts_nested_attributes_for
I have the following model: public class Product { public int Id { get;
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.