A number is a number, and we don’t need to attach meaning to it.
Why do you need to have continuous ids unique to each clinic?  As a developer, 
I would object to anyone forcing such a requirement.  If they insisted, then I 
would add a property that is calculated each time based on the count of 
patients in the clinic.

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Joel Mathew
Sent: Wednesday, October 17, 2018 11:01 AM
To: django-users@googlegroups.com
Subject: Creating seperate unique ids within the same table

I have an application which is a clinic management software which allows login 
into different clinics, and needs to keep patient records of each clinic 
seperately. The patient records are filed in one common model, with a 
ForeignKey referring to the clinic to which a patient belongs.

Hitherto I had used the AutoField primary key of the customer model as a 
hospital identification number for the patient. This number is unique and used 
throughout the application to retrieve querysets related to the customer. My 
problem is this. I want each patient to have a unique automatically 
incrementing id which can be given to them. Obviously since all patient records 
(irrespective of which clinic the patient belongs to) are stored in one model, 
the ids are not seperated by clinic. cstid 1 may be of clinic 1, cstid 5 may 
again be of clinic 1, but cstid 6 may be of clinic 2.

I need patients  of one clinic to have continuous ids unique to each clinic. 
What can I do to achieve this, within my defined models?

    class Clinic(models.Model):
        clinicid = models.AutoField(primary_key=True, unique=True)
        name = models.CharField(max_length=60, unique=True)
        label = models.SlugField(max_length=25, unique=True)
        email = models.EmailField(max_length=50, default='')
        mobile = models.CharField(max_length=15, default='')
        alternate = models.CharField(max_length=15, default='', blank=True)
        about = models.CharField(max_length=250, blank=True)
        state = models.CharField(max_length=25)
        city = models.CharField(max_length=35)
        locality = models.CharField(max_length=35)
        pincode = models.IntegerField(default=0)
        address = models.TextField(max_length=80, default='', blank=True)
        website = models.URLField(blank=True)
        logo = models.ForeignKey(ProfilePic, blank=True, null=True, 
on_delete=models.CASCADE)
        latitude = models.FloatField(blank=True)
        longitude = models.FloatField(blank=True)
        placeurl = models.URLField(blank=True)
        class Meta:
            unique_together = ["name", "mobile", "email"]
        def __str__(self):
            return self.name<http://self.name>

    class customer(models.Model):
        # Need autoincrement, unique and primary
        cstid = models.AutoField(primary_key=True, unique=True)
        name = models.CharField(max_length=35)
        age=models.IntegerField()
        gender_choices = (
            ('male', 'Male'),
            ('female', 'Female'),
            ('other', 'Something else'),
            ('decline', 'Decline to answer'))
        gender = models.CharField(
            choices=gender_choices, max_length=10, default='male')
        maritalstatus_choices = (
            ('unmarried', 'Unmarried'),
            ('married', 'Married'))
        maritalstatus = models.CharField(
            choices=maritalstatus_choices, max_length=10, default='Unmarried')
        mobile = models.CharField(max_length=15, default='')
        alternate = models.CharField(max_length=15, default='', blank=True)
        email = models.CharField(max_length=50, default='', blank=True)
        address = models.CharField(max_length=80, default='', blank=True)
        city = models.CharField(max_length=25, default='', blank=True)
        occupation = models.CharField(max_length=25, default='', blank=True)
        bloodgroup_choices = (('apos', 'A+'),
            ('aneg', 'A-'),
            ('bpos', 'B+'),
            ('bneg', 'B-'),
            ('opos', 'O+'),
            ('oneg', 'O-'),
            ('abpos', 'AB+'),
            ('abneg', 'AB-')
            )
        bloodgroup = models.CharField(choices=bloodgroup_choices, max_length=5, 
default='-', blank=True)
        linkedclinic = models.ForeignKey(Clinic, on_delete=models.CASCADE)
        class Meta:
            unique_together = ["name", "mobile", "age", "linkedclinic"]
        def __str__(self):
            return self.name<http://self.name>

My thoughts are on counting the number of customer objects with specific 
ForeignKey of a clinic, and then updating hospital id field in the same model. 
But probably there's a more logical less tedious way of doing this? Or should I 
be creating seperate tables for each clinic?
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAA%3Diw_98pujPThLkq6FCqGPqJYkE1U4HF1RswQaGTv8QGc7xXw%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAA%3Diw_98pujPThLkq6FCqGPqJYkE1U4HF1RswQaGTv8QGc7xXw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/02ed2d48826e4249a66072c8f660e77a%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to