tags:

views:

380

answers:

3

Hi,

I have the following models

class Employee(Person):  
  job = model.Charfield(max_length=200)  
class Address(models.Model):
  street = models.CharField(max_length=200)
  city = models.CharField(max_length=200)
class EmpAddress(Address):
  date_occupied = models.DateField()
  date_vacated = models.DateField()
  employee = models.ForeignKey()

When I build a json data structure for an EmpAddress object using the django serialzer it does not include the inherited fields only the EmpAddress fields. I know the fields are available in the object in my view as I can print them but they are not built into the json structure.

Does anyone know how to overcome this?

Thanks
Andrew

A: 

Inheritance of Django models can get a little tricky. Unless you excplicitly require EmpAddress to be subclass of Address, you might just want to duplicate the fields and let duck typing handle the fact that you aren't following traditional object oriented design. E.g:

class Address(models.Model):
    street = models.CharField(max_length=200)
    city = models.CharField(max_length=200)

class EmpAddress(Address):
    street = models.CharField(max_length=200)
    city = models.CharField(max_length=200)
    date_occupied = models.DateField()
    date_vacated = models.DateField()
    employee = models.ForeignKey()

Another shot in the dark you might try is to use jsonpickle (I'm one of the developers), which is "smarter" than the standard json module. The latest code has some great new features, thanks to davvid.

John Paulett
These are not my models I am just working with them so they need to stay as they are. I will check out the jsonpickle though. Thanks.
Andrew Gee
A: 

Take a look at: http://www.partisanpost.com/2009/10/django-jquery-jqgrid-example-one/1/ as a solution to your problem. The full serializer allows you to drill down into foreignkey relationships as far as you need to go. I wrote a tutorial example of how to use it to integrate django with JqGrid, which provides an example of just what you are faced with. Hope this helps.

John
That is the example that I have based mine on an it does work for the Foreign Key but it ignores the inheritance. If I use the relations param in the serializer I get all of the employee data but I don't get the inherited fields from Address. I looked at the the code for the serializer and it seems to me that is is only working with the local fields in the model passed in. But I am still learning this so I could be misunderstanding that code.
Andrew Gee
Perhaps you could post your view code. This would be helpful in finding a solution.
John
View code added above
Andrew Gee
A: 

John,

This is view code I am using along with the models is;

def address_grid(request):
  employeeId = request.GET.get('employeeId')  

  if request.GET.get('sidx') == '':
    order = 'date_occupied'
  else:
    order = request.GET.get('sidx')

  if request.GET.get('sord') == 'asc':
    sort_order = ''
  else:
    sort_order = '-'

  order = sort_order + order

  if request.GET.get('page'):
    paginated = int(request.GET.get('page'))
  else:
    paginated  = 1

  items = int(request.GET.get('rows'))

  addresses = EmpAddress.objects.filter(employee__id=employeeId)
  for add in addresses:
    log.write(add.city+'\n') # Field from address object      

  total = adresses.all().count()

  if total % items > 0:
    items_sum = 1
  else:
    items_sum = 0

  pages = total / items + items_sum

  if paginated > pages:
    paginated = 1

  addresses = addresses.order_by(order)[paginated-1)*items:paginated*items]

  rows = serializers.serialize("json", addresses, indent=4,)

  addresses = '{total:%(pages)s, page:%(page)s, records:%(total)s, rows:%(addresses)s' \
            % {'pages':pages, 'page':paginated, 'total':total, 'addresses':rows}

  log.write(rows+'\n') #json object no Address fields (city is not included)  
                       #even it is present above  


  return HttpResonse(addresses, mimetype="application/json")

When I print the addresses objects after the
addresses = EmpAddress.objects.filter(employee__id=employeeId)
line I have all of the objects attributes (both the Address and EmpAddress fields).

But when I print the json object I only have the EmpAddress object attributes excluding the Address attributes.

Andrew Gee