Thursday, May 26, 2016

Re: How to select undetermined field(s) in django model?

You need to construct a dict with fieldname value pairs, like:

search = {'field1': value1, 'field2', value2}

And then you can do standard python kw-expansion: MyModel.objects.filter(**search)

Though I suspect you want to do that for arbitrary fields so you should filter out all bad stuff before doing such queries. Queries itself are safe from SQL injection.

On 26.05.2016 15:30, Ketan Bhatt wrote:

It just means the "field_name" you are using in the filter query doesn't exist.

Multiple reasons:
1. The model doesn't really have that field in its definition.
2. You forgot migrating your database.
3. The field name being sent from your form is not right.

To get a clearer picture, try the same filter query directly on Django shell ("Python manage.py shell") and check what happens.


On Thu, May 26, 2016, 5:54 PM Jianshu Hu <hujianshu999@gmail.com> wrote:
To all,

I am new to django, and learning to establish a site.

Here is my problem:
    I have got a databse and a table in mysql, and am using django to read the data from it.
    I got a pair of values [FIELD_NAME,VALUE] from the GET method of a html page. When the values passed to the searching function, I want to get the searching result.
    I knew how to do this in MySQLdb, but unable to achieve it in django model.
    In MySQLdb:
        cur.execute("SELECT COLUMN_A,COLUMN_B,COLUMN_C FROM mydatabase WHERE %s=%s",(filed_name,value))
    However, in django:
        results=mydatabase.objects.filter(filed_name=value)
    I got an error: Cannot resolve keyword 'filed1' into field. Choices are: Bla bla bla

   What should I do?

Bests.    
 
 
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bddb8755-33d2-4bb4-9769-3fb350a43ff0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACyRkBUFVBe2K5Ym4xokFdGdNvhFfN-8%3D54YFSdf_NEm_f_Ssw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment