import different entries using filter

39 views
Skip to first unread message

jon stan

unread,
Jun 20, 2017, 12:11:23 AM6/20/17
to Django users
hey im trying to import one set of info from a database based on the name of something else if that makes sense.

basically in the database its setup like this:

   food-----------------------------------------------------------------
      restaurant-name       products         type of service
         
         (burger-place)        (burgers)           (fast food)
   -----------------------------------------------------------------------

im trying to display the products and service type based on the name but i cant figure out how todo that. here's what i have:

     views.py
          name = food.objects.get(restaurant-name__icontains='burger-place')
          prod = food.objects.filter(restaurant-name__icontains='burger-place').filter(products__icontains='burgers')
          service = food.objects.filter(restaurant-name__icontains='burger-place').filter(service__icontains='fast food')

but i cant get it to work correctly. i get 'burger-place' for the name, prod, and service variables for some reason. would i have to create completely separate models or is there a way i can get these variables based on the restaurant name?

Melvyn Sopacua

unread,
Jun 20, 2017, 4:37:59 PM6/20/17
to django...@googlegroups.com

On Monday 19 June 2017 21:11:23 jon stan wrote:

 

> im trying to display the products and service type based on the name

> but i cant figure out how todo that. here's what i have:

>

> views.py

> name =

> food.objects.get(restaurant-name__icontains='burger-place') prod =

> food.objects.filter(restaurant-name__icontains='burger-place').filter(

> products__icontains='burgers') service =

> food.objects.filter(restaurant-name__icontains='burger-place').filter(

> service__icontains='fast food')

>

> but i cant get it to work correctly. i get 'burger-place' for the

> name, prod, and service variables for some reason.

 

You are confusing attributes and models ("fields vs tables"). Please revisit the Django tutorial.

 

--

Melvyn Sopacua

Derek

unread,
Jun 21, 2017, 6:12:07 AM6/21/17
to Django users
Your clue is in the word "variable" that you used.  The "variables" for a Django model are its fields; so what you are looking for is a value stored in your model's field.

e.g.

service = food.objects.get(restaurant-name__icontains='burger-place').values_list('type_of_service', flat=True)

This assumes that your Django "Food" model has a  field called 'type_of_service'.

Another way to do this would be to first get the restaurant object:

place = food.objects.get(restaurant-name__icontains='burger-place')

Then you can more get to the fields that you need quite easily:

print(place.type_of_service)
print(place.product)

But you may need to think about your database design to ensure the right data is being stored in the right models/fields e.g.

https://stackoverflow.com/questions/32366294/database-design-for-food-ordering-system-mysql
http://www.wellho.net/resources/ex.php4?item=s154/sql
Reply all
Reply to author
Forward
0 new messages