milandjukic88
milandjukic88

Reputation: 1115

Getting model default value from modelForm in django

I have something like this in models.py

class A(models.Model):
    filed1 = CharField('label',max_length=3,default="default_1")

and forms.py:

class AForm(ModelForm):
    class Meta:
        model = A

    def __init__(self,*args,**kwargs):
        super(AForm,self).__init__(*args,**kwargs)
        setAttrs(self)

and function setAttrs

def setAttrs(object):
    for field_key,field_value in object.fields.items():

With last for loop i can access form fields and it works. But my question is can i get related field from model and than get its default value?

NEW UPDATE

Related to this question. I have same classes but field is custom (CustomCharField). In that custom field I have attribute readOnly="True". Can i somehow access to this attribute from last for loop?

Upvotes: 0

Views: 280

Answers (1)

Rohan
Rohan

Reputation: 53326

In your for loop, you can access the default value using initial attribute of field_value.

def setAttrs(object):
    for field_key,field_value in object.fields.items():
        print field_value.initial

Above will print initial/default value of each field.

Upvotes: 1

Related Questions