On 30 oct, 14:26, Mark Wolgemuth <[EMAIL PROTECTED]> wrote:
> It seems like I'm missing something, but here's what I'm trying to do:
>
> In creating a Form object, I'm setting size limit validation
> (max_length). I'd like to recover the value for that from the
> corresponding Model's Field's max_length. So basically I want a symbol
> lookup that will inspect in the options of a field I have defined for
> a model, without having an instance of the model, just it's type.
>
> This is to avoid the 2 having to share a common separately defined
> constant, or just using an integer that I have to coordinate in both
> places, or in place of always using auto forms.
>
> eg
>
> class MyModel(Model):
>      myfield = CharField(max_length=128)
>
> ------
>
> class MyForm(Form):
>      myfield = CharField(max_length=**GET.FROM.MODEL**)
>

def get_model_field_option(model, field, option, default=None):
    field = model._meta.get_field_by_name(field)[0]
    return getattr(field, option, default)

class MyForm(Form):
     myfield = CharField(
         max_length=get_model_field_option(MyModel, 'myfield',
'max_length', 100)
         )

Caveat : Not sure it works on all possible options for all possible
fields - testing left as an exercise to to OP !-)

HTH
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to