Issue
I am working on a Ticketing project where I want the admin to be to generate multiple unique numeric PINs that customers can purchase and can be validated on the app for event registration. I have researched and realised that this could also be done using bulk create but don't know how. Here is my Ticket Model
class Ticket(models.Model):
name =models.CharField(max_length=50)
price = models.PositiveIntegerField()
pin = models.CharField(max_length=6)
def __str__(self):
return self.name
I want a situation where admin would be able to generate multiple PINs for a particular ticket with a click but don't know how to go about it so someone should please help with the best way of doing it.
Solution
You should create another model TicketPIN
which has a ForeignKey
relation to Ticket
model:
from random import randint
from django.db import models
def generate_pin():
return ''.join(str(randint(0, 9)) for _ in range(6))
class TicketPIN(models.Model):
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
value = models.CharField(max_length=6, default=generate_pin, blank=True)
class Meta:
unique_together = ["ticket", "value"]
def __str__(self):
return f"{self.ticket} | {self.value}"
You won't need the pin field inside the Ticket
model.
Next, you should create a view that only superuser can access:
from django.http import JsonResponse
from .models import Ticket, TicketPIN
def generate_pins_for_ticket(request, ticket_id):
if request.user.is_superuser:
ticket = Ticket.objects.get(id=ticket_id)
for _ in range(20):
TicketPIN.objects.create(ticket=ticket)
return JsonResponse({"message": f"Created 20 PINs successfully for {ticket} ticket."})
return JsonResponse({"error": "Not Authorized"})
And in urls.py
:
from django.urls import path
from .views import generate_pins_for_ticket
urlpatterns = [
path("generate-pins/<int:ticket_id>", generate_pins_for_ticket),
]
Let's say the admin created a ticket that has id=1
, when the admin is logged out, any user that visits /generate-pins/1
will see the response {"error": "Not Authorized"}
. Only if the admin is logged in and he visits /generate-pins/1
will generate 20 PINs. you can modify the view to accept any number to generate and create a form so it's admin-friendly.
Answered By - 麻美雫
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.