我正在尝试创建员工列表。我可以为该员工分配用户名,密码和电子邮件,也可以更新员工信息,我可以更新用户名,密码或电子邮件。我可以创建一个员工,同时创建用户名和密码以及更新员工信息,但在更改员工的用户名或密码或电子邮件时,会创建一个新用户。
这是我试过的
def edit_employee(request, id):
instance = get_object_or_404(Employee, id=id)
form = EmployeeForm(request.POST or None, instance=instance)
if request.method == "POST" and form.is_valid():
employee = Employee.objects.get(id=id)
prev_username = employee.username
username = form.cleaned_data['username']
password = form.cleaned_data['password']
email = form.cleaned_data['email']
office_instance = OfficeSetup.objects.get(owner=request.user)
form = form.save(commit=False)
form.office = office_instance
# change username or password or email if admin has done the changes in
# those field
try:
user_instance = User.objects.get(username=prev_username)
user_instance.username = username
user_instance.set_password(password)
user_instance.email = email
form.save()
user_instance.save()
except User.DoesNotExist:
messages.error(request, "Following User Does Not Exist!")
except:
user = User.objects.create_user(
username=username, password=password, email=email)
form.save()
user.save()
messages.success(request, "Thank you")
return redirect('employee-list')
messages.warning(request, "Error")
context = {
'form': form
}
return render(request, 'dashboard/hrm/employee.html', context)
编辑部分
def edit_employee(request, id):
instance = get_object_or_404(Employee, id=id)
form = EmployeeForm(request.POST or None, instance=instance)
if request.method == "POST" and form.is_valid():
employee = Employee.objects.get(id=id)
prev_username = employee.username
username = form.cleaned_data['username']
password = form.cleaned_data['password']
email = form.cleaned_data['email']
office_instance = OfficeSetup.objects.get(owner=request.user)
form = form.save(commit=False)
form.office = office_instance
# change username or password or email if admin has done the changes in
# those field
user, created = User.objects.get_or_create(username=prev_username)
if created:
user.set_password(password)
user.email = email
form.save()
user.save()
return redirect('employee-list')
user.set_password(password)
user.email = email
form.save()
user.save()
return redirect('employee-list')
messages.warning(request, "Error")
context = {
'form': form
}
return render(request, 'dashboard/hrm/employee.html', context)
屏幕截图是为了使流程或用例清晰
更新代码
public async Task CopyDatabaseIfNotExists(string dbPath)
{
var nExpectedFolder = IsolatedStorageFile.GetUserStoreForApplication();
try
{
await ApplicationData.Current.LocalFolder.GetFileAsync(dbPath); //nExpectedFolder.GetFileAsync(dbPath);/// ApplicationData.Current.LocalFolder.GetFileAsync("preinstalledDB.db");
// No exception means it exists
return;
}
catch (System.IO.FileNotFoundException)
{
// The file obviously doesn't exist
}
(... some other stuff)
答案 0 :(得分:1)
get_or_create
方法适用于此类情况。我会重构做这样的事情:
user, created = User.objects.get_or_create(username=username)
if created:
# Fields to only populated on creation
user.password = password
...etc...
# Fields to update on creation or update
user.email = email
...etc...
user.save()
详细信息:https://docs.djangoproject.com/en/1.11/ref/models/querysets/#get-or-create
祝你好运!