English 中文(简体)
select_related() and many to many fields with ajax (Django)
原标题:

I have a simple view that I want to respond to both ajax and regular HTTP requests. Simplified, it looks like this:

def tag_search(request, tag):

    items = Item.objects.filter(tags__tagname__exact=tag)

    if request.is_ajax():

        return HttpResponse(serializers.serialize( json , items), mimetype= application/json )

    else:

        return render_to_response( mytemplate.html , locals())

The problem is that it isn t returning the values of the many to many relationships - just a list of the primary keys like:

[1, 2, 5]

I understand that I can t use select_related() to follow many to many relationships - can anyone provide me with a best practice for passing that information back, or an example?

最佳回答

Update - it seems Django doesn t support this particularly well, but there s a third party serializer that does:

DjangoFullSerializers

问题回答

I have written some code to do serialization in my project. It serializes model objects into dictionaries based upon a context, which describes how to serialize objects of each encountered type, so you may drop some fields from serialization or add new fields not present in the model. The code lacks comments, but you can find usage samples in unit tests. Hope that helps.

You may want to do a bulk select using those ids (probably the easiest solution)

item_ids = [1, 2, 5]
Item.objects.in_bulk(item_ids)

# Another option:
Item.objects.filter(id__in=item_ids)

edit: My advice is to either use django-tagging which handles this for you. OR just add a method to your Item model that gets the tags (and uses cache liberally)

from django.core.cache import cache

class Item(models.Model):
    ... 

    def get_tags(self):
        cache_key = "item_%s_tags" % self.id
        cache_timeout = 600  # 10 minutes or whatever

        tags = cache.get(cache_key, False)
        if not tags:
            tags = self.tags.all()
            cache.set(cache_key, tags, cache_timeout)

        return tags




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签