如何在Django中将客户分配给用户? [英] How to assign a customer to a user in Django?

查看:64
本文介绍了如何在Django中将客户分配给用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个系统,在这个系统中,有多个用户和客户.我要创建一个分配函数.客户应分配给用户.

I created a system and in this system, there are several users and customers. What I want to create an assign function. A customer should assign to a user.

例如,我有一个客户列表.当用户单击按钮时,该用户将看到其他用户的列表并选择其中一个.之后,客户名称将列在所选用户的其他已分配客户列表中.

For example, I have a customer list. When the user clicks a button, the user will see a list that other users and select one of them. After that, the customer's name will be listed in the different assigned customers list of the selected user.

我写了一个代码,并且可以正常工作,但是我无法从客户列表中接触到该用户.我的意思是,当我单击分配"按钮时,它会创建一个新客户.如何获取用户ID或用户名?

I wrote a code and this is working, but I cannot reach user from customer list. What I mean is when I click assign button it create a new customer. How can I reach user id or username?

views.py

def customer_list(request):
    current_user = request.user
    userP = UserProfile.objects.get_or_create(username=current_user)
    customer_list = Customer.objects.filter(company=userP[0].company.comp_name)

    # Assign
    form = AssignForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('user:customer_list')

    myFilter = TableFilter(request.GET, queryset=customer_list.all())

    context = {
        'customer_list': customer_list,
        'myFilter': myFilter,
        'form': form

    }
    return render(request, 'customer_list.html', context)

models.py

class Customer(models.Model):

    customer_name = models.CharField(max_length=20)
    country = models.CharField(max_length=20)
    address = models.CharField(max_length=100)
    VATnumber = models.CharField(max_length=10)
    telephone = models.CharField(max_length=10)
    email = models.CharField(max_length=30)
    contact_person = models.CharField(max_length=30)
    company = models.CharField(max_length=20)
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, blank=True, null=True)

class UserProfile(AbstractUser, UserMixin):
    company = models.ForeignKey(CompanyProfile, on_delete=models.CASCADE, null=True, unique=False)
    user_id = models.UUIDField(default=uuid.uuid4(), editable=False, unique=True)
    username = models.CharField(max_length=500, unique=True)
    first_name = models.CharField(max_length=200)

customer_list.py

<table id="multi-filter-select" class="display table table-striped table-hover grid_" >
                                    <thead>
                                        <tr>
                                            <th>Customer Name</th>
                                            <th>Country</th>
                                            <th>E-Mail</th>
                                            <th>Phone</th>
                                            <th>VAT Number</th>
                                            <th>Operations</th>
                                        </tr>
                                    </thead>
                                
                                    <tbody>
                                    {% for customer in customer_list %}
                                        <tr>
                                            <td>{{customer.customer_name}}</td>
                                            <td>{{customer.country}}</td>
                                            <td>{{customer.email}}</td>
                                            <td>{{customer.telephone}}</td>
                                            <td>{{customer.VATnumber}}</td>
                                            <td>
                                                <div class="row">
                                                    
                                                    <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo{{ forloop.counter }}">Assign</button>
                                                      <div id="demo{{ forloop.counter }}" class="collapse">
                                                        {% if customer.user == null %}
                                                            <form method="post">
                                                                {% csrf_token %}
                                                                {{ form|crispy }}
                                                                <button type="submit" class="btn btn-success ">Assign</button>
                                                            </form>
                                                        
                                                        {% else %}
                                                            Assigned to {{ customer.user.first_name }} {{ customer.user.last_name }}
                                                        {% endif %}
                                                      </div>


                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
</table>

forms.py

class AssignForm(forms.ModelForm):
    class Meta:
        model = Customer
        fields = ('user',)

推荐答案

views.py

def customer_list(request):
    current_user = request.user
    userP = UserProfile.objects.get_or_create(username=current_user)
    customer_list = Customer.objects.filter(company=userP[0].company.comp_name)
    # Assign
    form = AssignForm(request.POST or None)
    if request.POST:
        customer_id = request.POST.get('customer_id', None)
        customer = Customer.objects.get(id=customer_id)
        user = UserProfile.objects.get(id=request.POST.get('user', None))
        customer.user = user
        customer.save()
        form.save()
        return redirect('user:customer_list')
    context = {
        'customer_list': customer_list,
        'form': form

    }
    return render(request, 'customer_list.html', context)

customer_list.html

...
<button type="button" class="btn btn-success" data-toggle="collapse" data-target="#demo{{ forloop.counter }}">Assigned</button>
...
<form method="post">
     {% csrf_token %}
     {{ form|crispy }}
     <input type="hidden" name="customer_id" value="{{ customer.id }}">
     <button type="submit" class="btn btn-success ">Assign</button>
 </form>

这篇关于如何在Django中将客户分配给用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆