简体   繁体   English

如何在 Django model 中按 created_at 顺序递增序列号

[英]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:这是我的 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如果是这样,您应该能够使用 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如果您想修改该数字,以下是一些如何完成的示例: https://stackoverflow.com/a/16753988/1536402

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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