def search(self, pattern): qs = Contact.objects.filter(name__icontains=pattern) return { 'count': qs.count(), # use serialize if you want to exclude or define fields 'items': serialize(qs, excludes=['datetime_added',]), # inline objects are no problem without serialize 'first_item': (qs.count() > 0) and qs[0] or None, # define fields if you want to restrict output to specific fields 'second_item': (qs.count() > 1) and \ serialize(qs[1], fields=[re.compile('^name')]) or None }
def search(self, pattern): qs = Contact.objects.filter( name__icontains=pattern ) return { 'count': qs.count(), # use serialize if you want to exclude or define fields 'items': serialize(qs, excludes=['datetime_added',]), # inline objects are no problem without serialize 'first_item': (qs.count() > 0) and qs[0] or None, # define fields if you want to restrict output to specific fields 'second_item': (qs.count() > 1) and \ serialize(qs[1], fields=[re.compile('^name')]) or None }
def new(self, name, phone=None, fax=None): contact = Contact( name=name, phone=phone, fax=fax ) contact.save() return serialize(contact, excludes=[re.compile(r'^datetime_'),])
def new(self, name, phone=None, fax=None): obj, created = Author.objects.get_or_create(name='Testauthor') contact = Contact.objects.create( name=name, phone=phone, fax=fax, author=obj ) return serialize(contact, excludes=[re.compile(r'^datetime_'),], include_pk=True)
def new(self, name, phone=None, fax=None): contact = Contact(name=name, phone=phone, fax=fax) contact.save() return serialize(contact, excludes=[ re.compile(r'^datetime_'), ])