Django Custom User update with one to one to Customer model

I'm trying to create Update view for Customer model which have Onetoone relation with User(django model. After five hours and trying function base and class views I'm unable to get this working. Where am I making a mistake?

my models.py

    class Customer(Model):
        user = OneToOneField(User, on_delete=CASCADE)
        mobile = CharField(max_length=12,null=True)
        dob = DateField(null=True)
    
        def __str__(self):
            return self.user.username

my views.py

    class ProfileUpdateView(UpdateView):
        template_name = 'forms.html'
        form_class = AdminUserUpdateForm
        model = User
        success_url = reverse_lazy('controls')
    
        def get_object(self, queryset=None):
            return Customer.objects.get(pk=self.kwargs['pk']).user
            # Not working

    def customer_list_view(request):
        customer = Customer.objects.filter(user__groups__name='customer')
        premium = Customer.objects.filter(user__groups__name='premium')
        context = {'customer': customer, 'premium': premium}
        return render(request, 'customercontrol.html', context)

my forms.py

class AdminUserUpdateForm(UserChangeForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'groups']

    mobile = CharField(max_length=30)
    dob = DateField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'form-control'

    @atomic
    def save(self, commit=True):
        user = super().save(commit)
        mobile = self.cleaned_data['mobile']
        dob = self.cleaned_data['dob']
        customer = Customer.objects.get(user__pk=user.pk)
        customer.mobile = mobile
        customer.dob = dob
        if commit:
            customer.save()
        return user

my templates, where I get PK for the queries.

{% extends "base.html" %}

{% block content %}
    <h1 class="font1">Our premium customers:</h1>
    <table class="table table-dark">
  <thead>
    <tr>
      <th scope="col">User ID</th>
      <th scope="col"><a class="btn btn-secondary" href="">Username</a></th>
        <th scope="col"><a class="btn btn-secondary" href="">Name</a></th>
      <th scope="col"><a class="btn btn-secondary" href="">Email</a></th>
        <th scope="col"><a class="btn btn-secondary" href="">Phone</a></th>
        <th scope="col"><a class="btn btn-secondary" href="">Date of Birth</a></th>
    </tr>
  </thead>
  <tbody>

        {% for c in premium %}
        <tr>
            <td>{{c.id}}</td>
        <td>
        <a href="">{{ c.user.username }}</a></td>
        <td>{{c.user.first_name}} {{c.user.last_name}} </td>
        <td>{{ c.user.email}}</td>
        <td>{{ c.mobile}}</td>
        <td>{{ c.dob}}</td>
        <td><ul>{% for item in c.user.groups.all %}<li>{{ item}}</li>{% endfor %}</ul></td>
        <td><a class="btn btn-danger" href="{% url 'user-delete' c.id  %}">Delete</a></td>
        <td><a class="btn btn-success" href="{% url 'user-update' c.pk  %}">Update</a></td>
        </tr>
        {% endfor %}
  </tbody>
  </table>
    <h1 class="font1">Our customers:</h1>
    <table class="table table-dark">
  <thead>
    <tr>
      <th scope="col">User ID</th>
      <th scope="col"><a class="btn btn-secondary" href="">Username</a></th>
        <th scope="col"><a class="btn btn-secondary" href="">Name</a></th>
      <th scope="col"><a class="btn btn-secondary" href="">Email</a></th>
        <th scope="col"><a class="btn btn-secondary" href="">Phone</a></th>
        <th scope="col"><a class="btn btn-secondary" href="">Date of Birth</a></th>
    </tr>
  </thead>
  <tbody>

        {% for c in customer %}
        <tr>
            <td>{{c.id}}</td>
        <td>
        <a href="">{{ c.user.username }}</a></td>
        <td>{{c.user.first_name}} {{c.user.last_name}} </td>
        <td>{{ c.user.email}}</td>
        <td>{{ c.mobile}}</td>
        <td>{{ c.dob}}</td>
        <td><ul>{% for item in c.user.groups.all %}<li>{{ item}}</li>{% endfor %}</ul></td>
        <td><a class="btn btn-danger" href="{% url 'user-delete' c.id  %}">Delete</a></td>
        <td><a class="btn btn-success" href="{% url 'user-update' c.pk  %}">Update</a></td>
        </tr>
        {% endfor %}
  </tbody>
  </table>
{% endblock %}

the one with form

{% extends "base.html" %}
{% block content %}
    <form method="post">
        {% csrf_token %}
        <table>{{ form }}</table>
        <input type="submit" value="Submit" class="btn btn-primary">
    </form>
{% endblock %}

urls.py

path('<int:pk>/groupupdate', GroupUpdateView.as_view(), name='groupdate'),
path('customercontrol', customer_list_view, name='customercontrol'),

problem


Solution 1:

I found solution to my own problem, I had to add function into my view which is passing initial data into form:

def get_initial(self):
    initial = super().get_initial()
    initial['dob'] = Customer.objects.get(pk=self.kwargs['pk']).dob
    initial['mobile'] = Customer.objects.get(pk=self.kwargs['pk']).mobile
    return initial