Re: Public method for getting model field names in order of definition
> Thanks for the example, but that's not a ModelForm. > > Richard Another try, view.py: from django.forms.models import model_to_dict def view(request): data = Foo.objects.all()[0] print data.start form = Example(data=model_to_dict(data)) return render_to_response('t.html', {'form': form}) Where Example is a ModelForm for model Foo. Seems like there is no easy way to get access to the initial data of a field in a template. Example(instance=foo) will only populate the forms initial dict but not data. Should there be a similar property for initial in BaseForm as there is for data? Anssi Kääriäinen -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=.
Re: Public method for getting model field names in order of definition
On Mon, 2009-11-23 at 04:56 -0800, Anssi Kaariainen wrote: > Here is a working example. > data = {'field1': 'Foo', 'field2': 10} > form = Example(data=data) Thanks for the example, but that's not a ModelForm. Richard -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=.
Re: Public method for getting model field names in order of definition
Here is a working example. I am using svn version of Django, but IIRC this works also in 1.0. views.py: from django.shortcuts import render_to_response from django import forms class Example(forms.Form): field1 = forms.CharField() field2 = forms.IntegerField(label='Labeled') def view(request): data = {'field1': 'Foo', 'field2': 10} form = Example(data=data) return render_to_response('t.html', {'form': form}) t.html: {% for field in form %} {{ field.label }}{{field.data}} {% endfor %} On Nov 23, 2:14 am, Richard Laagerwrote: > On Sat, 2009-11-21 at 05:57 -0800, Anssi Kaariainen wrote: > > If I am not completely mistaken you can use your ModelForm with > > field.label and field.data to get read-only view of the model. > > I was trying to do something like this today and didn't have any luck. > Do you have a pointer to a working sample? > > Richard > > signature.asc > < 1KViewDownload -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=.
Re: Public method for getting model field names in order of definition
On Sat, 2009-11-21 at 05:57 -0800, Anssi Kaariainen wrote: > If I am not completely mistaken you can use your ModelForm with > field.label and field.data to get read-only view of the model. I was trying to do something like this today and didn't have any luck. Do you have a pointer to a working sample? Richard signature.asc Description: This is a digitally signed message part
Re: Public method for getting model field names in order of definition
On Wed, Oct 21, 2009 at 6:38 AM, David Chandek-Starkwrote: > one should (probably) use the > "attname" attribute of a field for its name rather than the "name" > attribute. Why's that? I'm using the "verbose_name" attribute of the field and capitalizing the first letter to match the admin interface's behavior. Richard -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=.
Re: Public method for getting model field names in order of definition
On Nov 21, 6:25 am, Rob Hudsonwrote: > But if you wanted to redisplay that information back to the user in a > read-only form, there is no iterative way to do so (as far as I know). > Instead you must specifically "hard code" each field: > > > Field 1 > {{ profile.field1 }} > > If I am not completely mistaken you can use your ModelForm with field.label and field.data to get read-only view of the model. Anssi Kääriäinen -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=.
Re: Public method for getting model field names in order of definition
On Wed, Oct 21, 2009 at 6:38 AM, David Chandek-Starkwrote: > > The values_list() query set method is useful for dumping data to CSV, > etc. However, I find that I often want to use it without specifying > the field names (to get them all) and yet also include the field names > as the first row in my data export. There is no "public" method for > getting all the names of a model's fields in the order of definition. > Model._meta.get_all_field_names() returns a sorted list, and one has > to read the source code to discover that one should (probably) use the > "attname" attribute of a field for its name rather than the "name" > attribute. The ValuesQuerySet superclass of ValuesListQuerySet by > default sets its field names with: > > [f.attname for f in self.model._meta.fields] > > So, my question is: Do folks think that it would be good to have a > public method for getting the field names in order of definition? I do, and have another use case in mind... For example, let's say you have a Profile model and you have a profile edit view which uses a ModelForm. In the edit template you can simply output the {{ form }} or iterate over the form: {% for field in form %} # output field {% endfor %} But if you wanted to redisplay that information back to the user in a read-only form, there is no iterative way to do so (as far as I know). Instead you must specifically "hard code" each field: Field 1 {{ profile.field1 }} This doesn't solve your use case, but I'd like to see some sort of ModelDisplay, which mimics ModelForm, and can be used in such a way to either specify select fields or exclude fields: class ProfileDisplay(ModelDisplay): class Meta: model = Profile exclude = ('user',) You could then pass this to your template and iterate over it: {% for field in profile_display %} {{ field.field_name }} {{ field.value }} {% endfor %} In the past I've discovered the model_to_dict in django/forms/models.py that could be used for this if only it used a SortedDict instead of a plain dict. With this minor change... @@ -121,7 +121,7 @@ def model_to_dict(instance, fields=None, exclude=None): # avoid a circular import from django.db.models.fields.related import ManyToManyField, OneToOneField opts = instance._meta -data = {} +data = SortedDict() for f in opts.fields + opts.many_to_many: if not f.editable: continue You could then, in your view, do: profile_display = model_to_dict(profile_obj, exclude=('id', 'user')) And in the template, do: {% for field_name, value in profile_display.items %} {{ field_name }} {{ value }} {% endfor %} Which I think would be a nice shortcut for template creators and also reduces code change as you adapt your models. All that said, I'm not sure if I'd propose this change for this purpose... or if we did the method might better live somewhere else. The model_to_dict method is used specifically for Forms as the docstring cites: Returns a dict containing the data in ``instance`` suitable for passing as a Form's ``initial`` keyword argument. Is this a use case others could use? -Rob -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=.
Public method for getting model field names in order of definition
The values_list() query set method is useful for dumping data to CSV, etc. However, I find that I often want to use it without specifying the field names (to get them all) and yet also include the field names as the first row in my data export. There is no "public" method for getting all the names of a model's fields in the order of definition. Model._meta.get_all_field_names() returns a sorted list, and one has to read the source code to discover that one should (probably) use the "attname" attribute of a field for its name rather than the "name" attribute. The ValuesQuerySet superclass of ValuesListQuerySet by default sets its field names with: [f.attname for f in self.model._meta.fields] So, my question is: Do folks think that it would be good to have a public method for getting the field names in order of definition? Thanks, David --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~--~~~~--~~--~--~---