blackwindow
blackwindow

Reputation: 437

How to call a form.py in another application

I have a model named NameModel in my app named Customer. I wanna reuse that model's one of the choices in my another app's model or forms.py. How can I do that. Following is the code of my form in Customer app. GENDERCHOICES = ( ( "", ("---")), (6, ("Male")), (7, ("Female")), (8, ("Notknown")), ) Please help.

Upvotes: 0

Views: 117

Answers (1)

Jesus Gomez
Jesus Gomez

Reputation: 1520

remember that GENDERCHOICES is either a class member of NameModel or a tuple defined at your Customer app models.py

just import it and use it as you normally would, in your models.py file:

from Customer.models import NameModel

ExampleModel(models.Model):
   type = models.CharField(max_length=1,choices=NameModel.GENDERCHOICES)

or ...

from Customer.models import GENDERCHOICES

ExampleModel(models.Model):
   type = models.CharField(max_length=1,choices=GENDERCHOICES)

if your tuple is not defined inside the NameModel class.

Cheers.

EDIT

to edit a tuple you have to convert it to a list append to it and then convert back to a tuple:

l = list(GENDERCHOICES)
l.append(((9,("Something else")))
GENDERCHOICES_EXTRA = tuple(l)

hope this helps

Upvotes: 1

Related Questions