简体   繁体   中英

How to increment a serial number in Django model with order by created_at

I'm developing a hospital project!

People can apply online appointment for a specific doctor and the people will get a serial number once done the process of online appointment.

Here is my model:

class Appointment(models.Model):
    doctor = models.ForeignKey(
        DoctApp, on_delete=models.CASCADE, related_name="appointments")
    fee = models.IntegerField(default=1000, validators=[
                              MaxValueValidator(2000)])
    name = models.CharField(max_length=220)
    phone = models.CharField(max_length=12, default="01700000000")
    age = models.IntegerField(validators=[MaxValueValidator(200)])
    gender = models.CharField(max_length=10, choices=gen_choises)
    address = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    pat_pic_date = models.DateField()
    serial_number  = models.IntegerField()


    def __str__(self) -> str:
        return f"""ID: {self.doctor.id} - {self.doctor.name} - Pat Name: {self.name}"""

Can you consider to share the auto-increment fields system to get serial the number fields?

It sounds like you're looking for the primary key of the newly created appointment record. If so, you should be able to retrieve that with self.id

If you'd like to modify that number, here are some examples of how that could be done: https://stackoverflow.com/a/16753988/1536402

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM