Reputation: 1339
I got this Resource and It's working fine and listing all the attributes from Employee's.
class EmployeeResource(ModelResource):
journey = fields.ForeignKey(WorkJourney, 'work_journey')
class Meta:
queryset = Employee.objects.all()
resource_name = 'employee'
authentication = BasicAuthentication()
I have a method writen on Employee's model Class that lists the phone number's from an Employee (Terrible code imo., i think it should be an attribute but i can't change it).
@property
def phones(self):
return u' / '.join([self.personal_phones or u'', self.institutional_phones or u''])
The point is to write a Resource method that access that Model method and list results with Employee's attributes..
Upvotes: 0
Views: 252
Reputation: 1040
If your Phone model look like this:
class Phone(models.Model)
employee = models.ForeignKey(Employee, related_name=phones)
Then you can get list of all phones for the employee with defining in your EmployeeResource ToManyRelation with phones:
class EmployeeResource(ModelResource):
phones = fields.ToManyField(PhoneResource, 'phones', full=True)
class Meta:
queryset = Employee.objects.all()
resource_name = 'employee'
authentication = BasicAuthentication()
Also with override dehydrate method you can customize data what will be send to the client side.
Custom view is another solution for sending customize data.
Upvotes: 1
Reputation: 1642
You should be able to create it as a read-only field in your resource:
phones = fields.CharField(attribute='phones', readonly=True)
If you don't set readonly=True
, Tastypie will try and set the field's value on insert/update.
Upvotes: 1