import textwrap from django.contrib.auth import get_user_model from django.db import models from tickets.models.ticket import Ticket class Event(models.Model): author = models.ForeignKey( get_user_model(), on_delete=models.CASCADE, blank=True, null=True, related_name="tickets_events" ) ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE) date = models.DateTimeField(auto_now_add=True) body = models.TextField(blank=True) def author_text(self): if self.author is not None: return str(self.author) @property def snippet(self): body_snippet = textwrap.shorten(self.body, width=35, placeholder="...") # Define here rather than in __str__ so we can use it in the admin list_display return "{author} - {snippet}...".format(author=self.author_text, snippet=body_snippet) def __str__(self): return self.snippet