0

I have 2 classes in forms.py, I want to access one of those classes inside another class. Something like below-

class A(forms.ModelForm):
filename = forms.FileField(label='Select a file')

  class Meta:
      model = File
      fields = ('filename',)

class B(forms.ModelForm):
filename = A

  class Meta:
      model = Rack
      fields = ('rack',)

Both of the classes are using different models (I cannot modify models.py). I want a field filename in class B that is sort of creating a widget from class A, basically want the same filename atrribute from class A in class B. How to achieve this?

user2715898
  • 921
  • 3
  • 13
  • 20

1 Answers1

0

You can't access the filename through the class because the attribute "filename" is associated with the objects of A, not the class A.

[See this answer for how to create class variables in Python, and you can see why it's impossible to access the form field filename like that.]

I don't see why you would want to get the filename field from class A, and I didn't understand what you meant by "basically want the same filename atrribute from class A in class B." If you simply want the same FileField, you could always just do:

filename = forms.FileField(label='Select a file')

in class B as well.

codeandfire
  • 445
  • 2
  • 9