0
votes

I am currently working on a Calendar in python django web framework. I am trying to print something and raising the following error:

TypeError: coercing to Unicode: need string or buffer, long found

I have seen people with similar problems but not with 'long' and a solution that works.... Traceback:

> Internal Server Error: /tande/holiday/ Traceback (most recent call
> last):   File
> "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line
> 149, in get_response
>     response = self.process_exception_by_middleware(e, request)   File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line
> 147, in get_response
>     response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py",
> line 23, in _wrapped_view
>     return view_func(request, *args, **kwargs)   File "/Users/ryoung/Documents/Database/prodman/tande/views.py", line 83, in
> holiday
>     cal = HolidayCalendar(my_holidays).formatmonth(year, month)   File "/Users/ryoung/Documents/Database/prodman/tande/views.py", line 397,
> in __init__
>     print holiday   File "/Library/Python/2.7/site-packages/django/db/models/query.py", line
> 237, in __repr__
>     return repr(data)   File "/Library/Python/2.7/site-packages/django/db/models/base.py", line
> 467, in __repr__
>     u = six.text_type(self) TypeError: coercing to Unicode: need string or buffer, long found

Here is my code:

class HolidayCalendar(HTMLCalendar):

    def __init__(self, holiday):
        super(HolidayCalendar, self).__init__()
        self.holiday = self.holiday_days(holiday)
        print holiday
        # for something in holiday:
        #     print something

    def holiday_days(self, holiday):
            field = lambda holiday: holiday.start_date.day
            return dict(
            [(day, list(items)) for day, items in groupby(holiday, field)]
            )

I am trying to see what self.holiday is setting as... I tried a for loop aswell because I thought it would be a list of items and it raised the same error. Here is the view that calls the above

@login_required
def holiday(request):
    # get persons username
    # get month and year today
    date_today = datetime.now()
    year = date_today.year
    month = date_today.month
    # get holiday objects in the current month
    my_holidays = Holiday.objects.order_by('start_date').filter(
        Q(start_date__year=year, start_date__month=month) | Q(end_date__year=year, end_date__month=month)
    )
    cal = HolidayCalendar(my_holidays).formatmonth(year, month)

    # forms... render template...etc

Thanks!

1
Please post the full traceback. - bruno desthuilliers
Edit made above :) - Ruth Young
The problem seems to be in your Holyday model's __str__ or __unicode__ method. Those methods should return resp. a string or unicode object, it looks like it's returning a long (possibly the pk - wild guess...). - bruno desthuilliers
Ah yes you're right I had def __unicode__ (self): return self.id I've changed it to description now. Thanks! - Ruth Young

1 Answers

1
votes

The problem seems to be in your Holyday model's str or unicode method. Those methods should return resp. a string or unicode object, it looks like it's returning a long (possibly the pk - wild guess...)