Django Custom User update with one to one to Customer 模型

Django Custom User update with one to one to Customer model

我正在尝试为与用户有 Onetoone 关系的客户模型创建更新视图(django 模型。五个小时后并尝试函数基础和 class 视图我无法使它工作。在哪里我是不是搞错了?

我的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

我的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)

我的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

我的模板,我在其中获取查询的 PK。

{% 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 %}

形式

{% 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'),

我找到了解决我自己问题的方法,我不得不在我的视图中添加函数,该函数将初始数据传递到表单中:

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