Plain is headed towards 1.0! Subscribe for development updates →

 1import uuid
 2
 3from plain import models
 4
 5
 6@models.register_model
 7class Pageview(models.Model):
 8    uuid = models.UUIDField(default=uuid.uuid4)
 9
10    # A full URL can be thousands of characters, but MySQL has a 3072-byte limit
11    # on indexed columns (when using the default ``utf8mb4`` character set that
12    # stores up to 4 bytes per character). The ``url`` field is indexed below,
13    # so we keep the length at 768 characters (768 × 4 = 3072 bytes) to ensure
14    # the index can be created on all supported database backends.
15    url = models.URLField(max_length=768)
16    timestamp = models.DateTimeField(auto_now_add=True)
17
18    title = models.CharField(max_length=512, required=False)
19    # Referrers may not always be valid URLs (e.g. `android-app://...`).
20    # Use a plain CharField so we don't validate the scheme or format.
21    referrer = models.CharField(max_length=1024, required=False)
22
23    user_id = models.CharField(max_length=255, required=False)
24    session_key = models.CharField(max_length=255, required=False)
25
26    class Meta:
27        ordering = ["-timestamp"]
28        indexes = [
29            models.Index(fields=["timestamp"]),
30            models.Index(fields=["user_id"]),
31            models.Index(fields=["session_key"]),
32            models.Index(fields=["url"]),
33        ]
34        constraints = [
35            models.UniqueConstraint(
36                fields=["uuid"], name="plainpageviews_pageview_unique_uuid"
37            ),
38        ]
39
40    def __str__(self):
41        return self.url