KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I've recently started using the Django REST Framework (and Django, and Python - I'm an RTOS/embedded systems person!) in order to implement a RESTful Web API. Haven't had any problems yet which couldn't be resolved with Google, but this one has had me stumped for a few hours now. I have an embedded system which listens for events which are associated with a range of devices - analogous to a Phone making Calls, which is what I'll discuss here for brevity. A Phone has a number and a whole lot of Calls (that it has made) associated with it. A Call has an associated Phone (the Phone which made the Call) and a time of creation. When a Call occurs, it should be POSTed to the API. I have an embedded system which listens for Calls and their originating phone number, and submits them to the API. Since the embedded system knows the phone number, I would like it to submit: {"srcPhone":12345678} rather than {"srcPhone":"http://host/phones/5"} . This avoids the need for my embedded system to know the primary key of every Phone (or to GET Phones by number every time it wants to submit a Call). Google and the Django docs suggested I could achieve this with natural keys. My attempt follows: models.py from django.db import models from datetime import datetime from pytz import timezone import pytz from django.contrib.auth.models import User # Create your models here. def zuluTimeNow(): return datetime.now(pytz.utc) class PhoneManager(models.Manager): def get_by_natural_key(self, number): return self.get(number=number) class Phone(models.Model): objects = PhoneManager() number = models.IntegerField(unique=True) #def natural_key(self): # return self.number class Meta: ordering = ('number',) class Call(models.Model): created = models.DateTimeField(default=zuluTimeNow, blank=True) srcPhone = models.ForeignKey('Phone', related_name='ca
Tags (comma-separated)
Save Edits
Cancel