On 7 déc, 21:48, "sotiris.kazakis" <[EMAIL PROTECTED]> wrote:
> hello,
>
> I search a lot about the use of object.values() but don't get what I
> want.
>
> I want to put dynamically (with a string ?)

Better to use a sequence of strings IMHO.

> the fields that I want to
> get from a model. i.e.:
>
> #model
> Class Test(model.Model)
>    Id = models.AutoField(primary_key=True, verbose_name="a/a")
>    Name = models.CharField(max_length=90, unique=True,
> verbose_name="Name")
>    Description = models.CharField(max_length=24,
> verbose_name="Description")
>
> #function to view the data of my table with strFld
> def tbView(tbName,strFld=None):
>      tableName=eval(tbName)                       # class instance

eval() is the wrong solution for 99.9% use cases. If you want a
generic solution, better to use "app_label" "model_name" as argument
and db.models.loading.get_model (cf below).

>      tbFields=tableName._meta.fields            # load table fields

you actually don't use this in the rest of the code...

>     tbData=tableName.objects.values(strFld)  #load only field in
> strFld
>
>     return render_to_response('myView.html',
> {'tbData':tbData,'name':tbName})
>
> I would like to get the data from selected fields with this :
>
> strFld="Name,Description"
> tbView('Test',strFld)
>
> When I use that get this error :
>
> raise FieldError("Cannot resolve keyword %r into field. "
> FieldError: Cannot resolve keyword 'Name,Description' into field.
> Choices are: Id, Name, Description.
>
> What is wrong ?

You need to pass a sequence of field names. Which you can build from
strFld:

# assuming ',' is the delimiter:
  fieldnames = filter(None, [n.strip() for n in strFld.split(',')])


Now note that the HTTP protocol allow multiple values for a same key
in a GET or POST request, which is of course supported by Django's
request.GET, request.POST and request.REQUEST QueryDict objects.

here's a possible fix (untested, and without error handling).
<OT>
I allowed myself to use a more pythonic naming scheme (cf Python's
naming conventions: http://www.python.org/dev/peps/pep-0008/)
</OT>


from django.db.models.loading import get_model

def table_view(request, app_label, model_name):
    # example url with querystring:
    # /myproject/table_view/app_label/model_name/?field=foo&field=bar

    model = get_model(app_label, model_name)
    fields = request.REQUEST.getlist('field')
    data = model.objects.all().values(*fields)
    context = dict(data=data, app_label=app_label,
model_name=model_name)
    return render_to_response('my_view.html', context)

HTH
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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