Django Meta Class Access Outer Attribute

bobleujr

Folks,

I need to implement a form that may vary a little depending on a variable. My class that subclasses ModelForms looks like this

class ConstantVwModelForm(forms.ModelForm):
  #couple attributes

  def __init__(self, hasData, *args, **kwargs): 

  class Meta:
    fields = ('xx', 'yy' ..)

I am looking for the very best way to access the variable hasData from the inner class Meta, it would be like

class ConstantVwModelForm(forms.ModelForm):
  #couple attributes

  def __init__(self, hasData, *args, **kwargs): 

  class Meta:
    if hasData:
      fields = ('xx', 'yy', ..)
    else:
      fields = ('hh', ..)

Any help is highly appreciated

Shang Wang

You shouldn't do that and there's no way to achieve this. You can delete field on the fly in your __init__ function explicitly:

class ConstantVwModelForm(forms.ModelForm):
    #couple attributes
    def __init__(self, hasData, *args, **kwargs):
        if hasData:
            del self.fields['hh']
        else:
            del self.fields['xx']
            del self.fields['yy']

    class Meta:
        model = ConstantVwModel

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I access class Meta as a attribute of Django Model Class?

From Dev

Django: AttributeError: class Meta has no attribute 'model'

From Dev

Django: AttributeError: class Meta has no attribute 'model'

From Dev

Django: access the model meta class value

From Java

Django: FileField' object has no attribute 'attrs' in class Meta

From Dev

Access a class attribute using django in Python

From Dev

How to access outer outer class(not outer class) property?

From Dev

Access members of outer class in TypeScript

From Dev

Access outer class in inner nested class

From Dev

Inner class access to outer class nested enum

From Dev

Access outer class in inner nested class

From Dev

Inner class access to outer class nested enum

From Dev

Cannot access to attribute of class

From Dev

Not able to access a class attribute

From Dev

DjangoRestFramework Class Serializers missing "Meta" attribute?

From Dev

'function' object has no attribute 'META' in Django 1.7?

From Dev

Django 'AnonymousUser' object has no attribute '_meta'

From Dev

'function' object has no attribute 'META' in Django 1.7?

From Dev

Django AttributeError: 'Model' object has no attribute '_meta'

From Dev

'unicode' object has no attribute '_meta' with Django

From Dev

How to access outer class variable of same name?

From Dev

php - Access outer class from an anonymous callback

From Dev

How to access "this" reference of anonymous outer class in java

From Dev

Java Inner class - access to the variables of outer object

From Dev

Java - How to access outer class from argument?

From Dev

Access Meta-Annotation inside Class (TypeScript)

From Dev

Can't access class attribute

From Dev

Python Class dynamic attribute access

From Dev

Access the declaring class in a custom attribute?

Related Related

  1. 1

    Why can't I access class Meta as a attribute of Django Model Class?

  2. 2

    Django: AttributeError: class Meta has no attribute 'model'

  3. 3

    Django: AttributeError: class Meta has no attribute 'model'

  4. 4

    Django: access the model meta class value

  5. 5

    Django: FileField' object has no attribute 'attrs' in class Meta

  6. 6

    Access a class attribute using django in Python

  7. 7

    How to access outer outer class(not outer class) property?

  8. 8

    Access members of outer class in TypeScript

  9. 9

    Access outer class in inner nested class

  10. 10

    Inner class access to outer class nested enum

  11. 11

    Access outer class in inner nested class

  12. 12

    Inner class access to outer class nested enum

  13. 13

    Cannot access to attribute of class

  14. 14

    Not able to access a class attribute

  15. 15

    DjangoRestFramework Class Serializers missing "Meta" attribute?

  16. 16

    'function' object has no attribute 'META' in Django 1.7?

  17. 17

    Django 'AnonymousUser' object has no attribute '_meta'

  18. 18

    'function' object has no attribute 'META' in Django 1.7?

  19. 19

    Django AttributeError: 'Model' object has no attribute '_meta'

  20. 20

    'unicode' object has no attribute '_meta' with Django

  21. 21

    How to access outer class variable of same name?

  22. 22

    php - Access outer class from an anonymous callback

  23. 23

    How to access "this" reference of anonymous outer class in java

  24. 24

    Java Inner class - access to the variables of outer object

  25. 25

    Java - How to access outer class from argument?

  26. 26

    Access Meta-Annotation inside Class (TypeScript)

  27. 27

    Can't access class attribute

  28. 28

    Python Class dynamic attribute access

  29. 29

    Access the declaring class in a custom attribute?

HotTag

Archive