'Django usage of ForeignKey
I have this micro ticket "system"
\\models.py
class Ticket(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
default=None,
null=True,
on_delete=models.CASCADE,
)
title = models.CharField(max_length=200)
description = models.TextField()
creator_adress = models.GenericIPAddressField(null=True)
start_date = models.DateTimeField(default=timezone.now)
ticket_waiting = models.BooleanField(default=True)
ticket_solved = models.BooleanField(default=False)
reopened_counter = models.IntegerField(default=0)
...
Until now, only one message (when the user opens the ticket) can be sent by the user, which will be forwarded to the admin. But the admin and the user can't write any other messages back and forth under the ticket yet.
If you use a new model for this, which is connected to every single ticket like so?
class TicketMessages(models.Model):
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
admin_message = models.TextField()
user_message = models.TextField()
What is the best way to implement this?
Thank you very much :>
Solution 1:[1]
you can achieve this efficiently with the following method,
create a model for ticket message :-
class TicketMessages(models.Model):
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
message = models.TextField() #this will be the message
message_by = # you can use a foreign key or a choices option to select between the admin or user
reply_to = models.ForeignKey("self") # if this is a reply to existing message
This is kind of an good and efficient solutions as per my understanding of your problem .... if you need something else or something more, let me know I'd love to help you.
Thanks !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | NISHANT Pacharne |
