#Get next primary key for CreateView

1 messages · Page 1 of 1 (latest)

white chasm
#

Hello I wanted to display the next available primary key for the table that the form is creating. There will be other forms that they will need to select that value from a drop down.

my current view class:

class AreaForm(forms.ModelForm):
    @staticmethod
    def get_next_id():
        objs = Area.objects.all()
        ret = objs.aggregate(Max('area_id'))['id__max'] + 1 if objs else 1
        return ret

    area = forms.CharField(max_length=50, initial=get_next_id)
    area.disabled = True
    short_name = forms.CharField(max_length=50)

    class Meta:
        model = Area
        fields = '__all__'

however everything Ive tried here the value in the textbox is always the object (<staticmethod object at 0x0000021D565CC760>) instead of the actual value

proven light
#

This is a recipe for pain. The next primary key is difficult to determine reliably especially when you have multiple users attempting to create data at the same time. You may want to rework your user experience to result in a easier technical solution.

white chasm
#

There really wont be multiple users

proven light
#

What about multiple tabs or windows?