from looking at the fields_for_model(...) method in django.forms.model, it
doas
opts = model._meta
for f in opts.fields + opts.many_to_many:
# <some code with each field>
for each field it ends up calling f.formfield() which in CharField is
implemented as:
def formfield(self, **kwargs):
defaults = {'max_length': self.max_length}
defaults.update(kwargs)
return super(CharField, self).formfield(**defaults)
So, in conclusion, you can use the _meta attribute to inspect the model
fields, and CharField have a public attribute called max_length you can use
when creating your form.
On Wed, Jul 21, 2010 at 17:53, Rex <[email protected]> wrote:
> Hi All,
> I am using Django 1.1, say I have a model like:
> class MyModel(Model):
> someField = CharField(max_length=32)
>
> if I have another class that receives an instance of the model, can I
> read the max_length there?
>
> class Hypothetical_form (forms.Form):
> def __init__(self, model_instance):
> self.someField = forms.CharField(label = u'Some Label', widget
> = forms.TextInput(attrs={'size':
> this.magicalGetSize(model_instance,'someField')}))
>
> def magicalGetSize(instance, fieldname):
> return some_how_find_the_max_length(instance, fieldname)
>
> this will give me the ability to keep my db and forms sync.
> Thank you for any help
> Rex
> disclaimer: I know about ModelForm and all that, but I want to see if
> there is a way to inspect a Model's meta data and field options. I
> found functions like get_field_by_name() but they are not in the
> django documentation.
>
> --
> 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]<django-users%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>
--
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.