Jerry Bi
Jerry Bi

Reputation: 341

Making a custom id field in Django models

I'm trying to make a model in Django that has a custom id attribute. I want it to always equal the sum of 10000 and the current id number of that instance. How exactly do I write that? And do I have to do anything in the view?

Edit: I better put the code I'm using just in case.

models.py

class Schedules(models.Model):
    course_name = models.CharField(max_length=128, choices=COURSE_NAME_CHOICES, default='a-plus')
    location = models.CharField(max_length=128, choices=LOCATION_CHOICES, default='south_plainfield')
    room = models.CharField(max_length=128, choices=ROOM_CHOICES, default='A')
    start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today)
    start_time = models.CharField(max_length=128, choices=START_TIME_CHOICES, default='eight-thirty am')
    end_time = models.CharField(max_length=128, choices=END_TIME_CHOICES, default='eight-thirty am')
    instructor = models.CharField(max_length=128, choices=INSTRUCTOR_CHOICES, default='adewale')
    total_hours = models.CharField(max_length=128, choices=TOTAL_HOURS_CHOICES, default='six')
    hours_per_class = models.CharField(max_length=128, choices=HOURS_PER_CLASS_CHOICES, default='four_and_half')
    frequency = models.CharField(max_length=128)
    status = models.CharField(max_length=128, choices=STATUS_CHOICES)
    interval = models.CharField(max_length=128, choices=INTERVAL_CHOICES, default='1 day')
    initiated_by = models.CharField(max_length=128, null=True)
    schedule_id = models.IntegerField(default=0)

views.py

def start_One_Schedule(request):
    form = ScheduleForm()
    if request.method == 'POST':
        form = ScheduleForm(request.POST)

        if form.is_valid():
            obj = form.save(commit=False)
            obj.initiated_by = request.user.username
            obj.save()
            return render(request, 'schedule/schedule.html', {})
        else:
            print(form.errors)

    return render(request, 'schedule/start_one_schedule.html', {'form': form})

Upvotes: 4

Views: 2858

Answers (2)

Muhammad Hassan
Muhammad Hassan

Reputation: 14391

You can write custom id field in you model like this

class ABC(models.Model):
    custom_id = models.IntegerField(default=0)

    def save(self, flag=True, *args, **kwargs):
        # Save your object. After this line, value of custom_id will be 0 which is default value
        super(ABC, self).save(flag=True, *args, **kwargs)
        # Here value of custom_id will be updated according to your id value
        if flag:        
            self.custom_id = self.id + 10000
            self.save(flag=False, *args, **kwargs)

flag is required as otherwise it will start recursion which will run infinitely.

Upvotes: 2

schillingt
schillingt

Reputation: 13731

Create a property on the model that adds 10000 to the id field. You don't need to create the actual field.

class Example(models.Model):
    @property
    def custom_id(self):
        return self.id + 10000

Upvotes: 5

Related Questions