I create a customer model and a function for assign a customer to a user. Assign function is update user
field in Customer
model.
This Customer
model has a country
field. I want to assign the customers in the same country to a user with one form. For doing that I have listing all countries and a form for assign operation? How can I do that?
Edit: Now, I can get country name and the user for assigning. How can I use these attributes and update the objects ( Customer
) with chosen country and user.
models.py
class Customer(models.Model):
customer_name = models.CharField(max_length=20)
country = models.ForeignKey(Country, on_delete=models.CASCADE, null=True, unique=False)
...
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, blank=True, null=True)
class Country(models.Model):
REGION = [
('Northwest Europe', 'Northwest Europe'),
('Southwest Europe', 'Southwest Europe'),
...
]
country_name = models.CharField(max_length=30)
country_code = models.CharField(max_length=5)
views.py
def country_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)
countries = Customer.objects.values_list('country__country_name', flat=True).distinct()
form = CountryForm(request.POST or None)
if request.POST:
country_form = request.POST.get('country_form', None)
user = request.POST.get('user', None)
form.save()
print(country_form)
print(user)
return redirect('user:customer_countries')
context = {
'customer_list': customer_list,
'countries': countries,
'form': form
}
return render(request, 'country_customer_list.html', context)
country_customer_list.html *
<table id="multi-filter-select" class="display table table-striped table-hover grid_" >
<thead>
<tr>
<th>Country</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
{% for country in countries %}
<tr>
<td>{{country}}</td>
<td>
<button type="button" class="btn btn-info btn-block" data-toggle="collapse" data-target="#demo{{ forloop.counter }}"> Assign </button>
<div id="demo{{ forloop.counter }}" class="collapse">
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="hidden" name="country_form" value="{{ country }}">
<button type="submit" class="btn btn-primary btn-sm">Assign</button>
</form>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
forms.py
class CountryForm(forms.ModelForm):
class Meta:
model = Customer
fields = ('user',)
Populate Country models with your choise, and they will appear in the template
Change models
class Country(models.Model):
country_name = models.CharField(max_length=30)
country_code = models.CharField(max_length=5)
def __str__(self):
return str(self.country_name) + ' ' + str(self.country_code)
forms.py
class CountryForm(forms.ModelForm):
class Meta:
model = Customer
fields = ('user','country',)
in template
{{form.country}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.