Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3691762
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:11:03+00:00 2026-05-19T04:11:03+00:00

My Django view method is below. I want to pass place_data as a response

  • 0

My Django view method is below. I want to pass place_data as a response from an HTTPRequest (within a getJSON call on the client side, but that’s irrelevant to the issue).

I can pass the dictionary fine until I include the event_occurrences, which is doing some behind the scenes work to pass a dictionary of events with start and end times.

def mobile_place_detail(request,place_id):

    callback = request.GET.get('callback', 'callback')
    place = get_object_or_404(Place, pk=place_id)
    event_occurrences = place.events_this_week()

    place_data = {
        'Name': place.name,
        'Street': place.street,
        'City': place.city,
        'State': place.state,
        'Zip': place.zip,
        'Telephone': place.telephone,
        'Lat':place.lat,
        'Long':place.long,
        'Events': event_occurrences,
    }
    xml_bytes = json.dumps(place_data)

    if callback:
        xml_bytes = '%s(%s)' % (callback, xml_bytes)
    print xml_bytes

    return HttpResponse(xml_bytes, content_type='application/javascript; charset=utf-8')

Here is the code attempting to do the serialization of the event_occurrences dictionary:

 def events_this_week(self):
    return self.events_this_week_from_datetime( datetime.datetime.now() )

 def events_this_week_from_datetime(self, now):

    event_occurrences = []
    for event in self.event_set.all():
        event_occurrences.extend(event.upcoming_occurrences())

    event_occurrences.sort(key=itemgetter('Start Time'))

    counter = 0
    while counter < len(event_occurrences) and event_occurrences[0]['Start Time'].weekday() < now.weekday():
        top = event_occurrences.pop(0)
        event_occurrences.insert(len(event_occurrences), top)
        counter += 1

    json_serializer = serializers.get_serializer("json")()
     return json_serializer.serialize(event_occurrences, ensure_ascii=False)
    return event_occurrences

The call to event.upcoming_occurrences references the function below:

def upcoming_occurrences(self):

        event_occurrences = []

        monday_time = datetime.datetime.combine(datetime.date.today() + relativedelta(weekday=MO), self.start_time)
        all_times = list(rrule(DAILY, count=7, dtstart=monday_time))

        weekday_names = ('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday')

        for idx, weekday in enumerate(weekday_names):
            if getattr(self, weekday):
                event_occurrences.append({
                    'Name': self.name,
                    'Start Time': all_times[idx],
                    'End Time': all_times[idx] + datetime.timedelta(minutes=self.duration)
                })

        return event_occurrences

This is giving me the following error:

Exception Type: AttributeError
Exception Value:    'dict' object has no attribute '_meta'

I realize I cannot just call json.dumps() on my event_occurrences object, but can’t figure out how to get around this serialization error (and this is my first time working with serialization in Python). Could someone please give me some direction as how and where the serialization needs to take place?

Thank you in advance!

UPDATE: Added function calls to help with clarity of question. Please see above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:11:04+00:00Added an answer on May 19, 2026 at 4:11 am

    Django’s serialization framework is for QuerySets, not dicts. If you want to just dump a dictionary to JSON, just use json.dumps. It can easily be made to serialize objects by passing in a custom serialization class – there’s one included with Django that deals with datetimes already:

    from django.core.serializers.json import DjangoJSONEncoder
    json.dumps(mydict, cls=DjangoJSONEncoder)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Django view points to a function, which can be a problem if you want
I am trying to make a search view in Django. It is a search
In my Django project I am using Product.objects.all().order_by('order') in a view, but it doesn't
I’ve got a brand new Django project. I’ve added one minimal view function to
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass
I have a Django form which I'm validating in a normal Django view. I'm
I have the following view that takes the value of q from a template:
Django comes with CSRF protection middleware , which generates a unique per-session token for
In Django's template language, you can use {% url [viewname] [args] %} to generate
Does Django have any template tags to generate common HTML markup? For example, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.