I want to do an readonly API endpoint with a limit/offset based pagination that return a list of items. thoses items come from 3 models.
Workflow
-
I receive a request on the API endpoint url, this request include limit and offset arguments
-
I make a query in 3 different tables (I filter the query to get only published = True, this field is present on each model), limiting the query with the offset/limit (each model has a timestamp_created = DateField(auto_now_add=True, that can be used for sorting) a have a result of x objects
-
with this ‘collection’ of objects, for each object I need to create a dictionary to map the fields present in the model with a unique JSON format (meaning that all models must ‘comply’ with this single JSON format). I also need to compute some data or generate uri that will be added to the dict.
-
In the for loop used above to convert the object data in a dict at the end of the conversion I add the dict to a list
-
When I have the list of dict I will it in view using the django.http.JsonResponse
Models
class Prescription(models.Model):
reference = models.CharField(max_length=15, unique=True)
slug = models.SlugField(default=slugify(reference), editable=False)
sub_code = models.ForeignKey("certificates.SubCode", on_delete=models.PROTECT)
object = models.TextField()
pdf_file = models.FileField(
upload_to="uploads/prescriptions/", default=None, null=True, blank=True
)
pdf_file_en = models.FileField(
upload_to="uploads/prescriptions/", default=None, null=True, blank=True
)
pdf_file_fr = models.FileField(
upload_to="uploads/prescriptions/", default=None, null=True, blank=True
)
pdf_file_nl = models.FileField(
upload_to="uploads/prescriptions/", default=None, null=True, blank=True
)
published = models.BooleanField(default=False)
timestamp_updated = models.DateTimeField(auto_now=True)
timestamp_created = models.DateField(auto_now_add=True)
comment = models.TextField(default=None, blank=True, null=True)
note = models.TextField(default=None, blank=True, null=True)
class Regulation(models.Model):
reference = models.CharField(max_length=15, unique=True)
slug = models.SlugField(default=slugify(reference), editable=False)
type = models.ForeignKey(Type, on_delete=models.PROTECT)
code = models.ForeignKey(
"certificates.Code",
on_delete=models.PROTECT,
default=None,
blank=True,
null=True,
)
sub_code = models.ManyToManyField("certificates.SubCode", default=None, blank=True)
object = models.TextField()
pdf_file = models.FileField(
upload_to="uploads/regulations/", default=None, null=True, blank=True
)
pdf_file_en = models.FileField(
upload_to="uploads/regulations/", default=None, null=True, blank=True
)
pdf_file_fr = models.FileField(
upload_to="uploads/regulations/", default=None, null=True, blank=True
)
pdf_file_nl = models.FileField(
upload_to="uploads/regulations/", default=None, null=True, blank=True
)
published = models.BooleanField(default=False)
timestamp_updated = models.DateTimeField(auto_now=True)
timestamp_created = models.DateField(auto_now_add=True)
comment = models.TextField(default=None, blank=True, null=True)
note = models.TextField(default=None, blank=True, null=True)
class Certificate(models.Model):
ACTIVE = 1
PENDING = 2
SUSPENDED = 3
REVOKED = 4
STATUS = (
(ACTIVE, "active"),
(PENDING, "pending"),
(SUSPENDED, "suspended"),
(REVOKED, "revoked"),
)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
company = models.ForeignKey("address_book.Company", on_delete=models.PROTECT)
contact_email = models.EmailField()
sub_code = models.ForeignKey(SubCode, on_delete=models.PROTECT)
status = models.PositiveSmallIntegerField(choices=STATUS, default=PENDING)
published = models.BooleanField(default=False)
custom_url = models.URLField(default=None, null=True, blank=True)
creation_date = models.DateField(default=now)
pdf_file = models.FileField(
upload_to="uploads/certificates/", default=None, null=True, blank=True
)
internal_id = models.CharField(max_length=25, unique=True)
timestamp_updated = models.DateTimeField(auto_now=True)
timestamp_created = models.DateField(auto_now_add=True)
comment = models.TextField(default=None, blank=True, null=True)
note = models.TextField(default=None, blank=True, null=True)