3

Getting this Error:- 'Restaurant' object has no attribute 'menu_here__starters'

I'm using Django-Excel Lib

In My Models.py

Class Restaurant(models.Model):

  name = models.CharField(max_length=20)
  area = models.CharField(max_length = 30)
  menu_here = models.ForeignKey(Menu)

Class Menu(models.Model):

  starters = models.CharField(max_length = 50)
  desserts = models.CharField(max_length = 50)

In my Views.py

def download_excel_4(request):

query_set = Restaurant.objects.all() # Foreign column is Menu
column_names = ['menu_here__starters','menu_here__desserts' ]
return excel.make_response_from_query_sets(
        query_set,
        column_names,
        'xls',
         file_name="Restaurant With Complete Menu Database"
        )

1 Answer 1

3

The make_response_from_query_sets takes the objects returned by query_set and displays it along with column names which should correspond with the field names of the objects.

The column_names acts like a filter displaying only the field names you want but it cannot further query after the objects are got. Therefore valid names are ['name', 'area', 'menu_here'].

An alternative would be to get objects as a dict with the related fields while querying using the .values() method and then using excel.make_response_from_records.

query_record = Restaurant.objects.all().values('name', 'area', 'menu_here__starters', 'menu_here__desserts')
return excel.make_response_from_records(
    query_record,
    'xls',
     file_name="Restaurant With Complete Menu Database"
    )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This answer is so simple!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.