How can I Generate 10 Unique digits in Model Form and Pass Form Context Variable in Django Class Based ListView


I am new to Django Class Based Views and I am working on a project where on the template I want to have Form for creating customer accounts on the left and list of existing customers on the right.
So far I have the list of existing customers displayed but for the form I don’t know how to pass its variable context to the same template, or it is not possible to Pass a Form that would be submitted inside a ListView Method. And I also want to generate unique account numbers of 10 Digits in ModelForm which I want the form field to be auto-filled and disabled
Here is my form code:

import secrets

#I want to Generate Account Number of 10 Digits but getting only 2

account = secrets.randbits(7)

#class for Customer Account Form

class CustomerAccountForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
    super().init(*args, **kwargs)

    self.fields['accountnumber'].initial = account

class Meta:
    model = Customer
    fields = ['accountnumber','surname','othernames','address','phone']

Code for my views (ListView)

class CustomerListView(ListView):
    model = Customer
    form_class = CustomerAccountForm
    template_name = 'dashboard/customers.html'
    #Function to get context data from queries
    def get_context_data(self, **kwargs):
        context_data = super().get_context_data(**kwargs)
        #Get Day of today from current date and time
        now =
        #Get the date today
        date_today =
        #Count Number of Withdrawals Today and passing in context
        context_data['count_withdrawals_today'] = Witdrawal.objects.filter(date__year=now.year, date__month=now.month,
        context_data['count_deposits_today'] = Deposit.objects.filter(date__year=now.year, date__month=now.month,
        context_data['count_accounts'] = Customer.objects.count()
        context_data['count_users'] = User.objects.count()
        #Calculate today Deposit Today
        context_data['total_deposit']= Deposit.objects.filter(date__year=now.year, date__month=now.month,'deposit_amount')).get('total_deposit') or 0
        #Calculate today Withdrawal Today
        context_data['total_withdrawal']= Witdrawal.objects.filter(date__year=now.year, date__month=now.month,'withdrawal_amount')).get('total_withdrawal') or 0
        return context_data

Someone should please help me on how this is properly done and the form would be submitted successfully. Thanks in anticipation for your answer.


After going through many tutorials and blogs on Django Class Based Views with ListViews for Forms, I discovered that ListViews was designed to populate Model items while FormViews is designed for creating and processing forms both can’t be used on one template. Although many developers have had a way around it with the use of Multiple Mixins but no best practice has be mentioned for or against their findings yet.
In these case, I concluded that for a Django Template to be able to process Model Form and at the same time Populate Database items, it needs not to use Class Based Views but Function Based Views.

Answered By – apollos

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published