Skip to content

Instantly share code, notes, and snippets.

@CITGuru
Created April 23, 2018 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CITGuru/568e86402b99c2d2c2c36cc65b01c363 to your computer and use it in GitHub Desktop.
Save CITGuru/568e86402b99c2d2c2c36cc65b01c363 to your computer and use it in GitHub Desktop.
from django.db import models
import uuid
from django.contrib.auth.models import User
# Create your models here.
def generate_ticket_id():
return str(uuid.uuid4()).split("-")[-1] #generate unique ticket id
class Ticket(models.Model):
title = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField()
category = models.ForeignKey("Category", on_delete=models.CASCADE)
ticket_id = models.CharField(max_length=255, blank=True)
created = models.DateTimeField(auto_now=True)
modified = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "{} - {}".format(self.title, self.ticket_id)
def save(self, *args, **kwargs):
if len(self.ticket_id.strip(" "))==0:
self.ticket_id = generate_ticket_id()
super(Ticket, self).save(*args, **kwargs) # Call the real save() method
class Meta:
ordering = ["-created"]
class Category(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField()
def __str__(self):
return self.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment