私は Django を初めて使用し、会社の従業員データを作成して表示するアプリを作成しています。現在モデル、新入社員フォーム、社員表表示、ログイン/ログアウト、全て動作しています。私は現在のリストの編集に取り組んでいます。行リンクにカーソルを合わせて pk (employeeid) を URL に渡すと、フォームが正しく入力されますが、manytomanyfields が入力されず、pk が増加し、エントリが重複します (データの変更以外)。
モデル/フォームには合計35個のフィールドがあり、フォームフィールドを手動で行った方法で非常に長いコードになるため(よりきれいなフォーマットを実現するため)、コードのサンプルのみを入れます。
#view.py #SEE EDIT BELOW FOR CORRECT METHOD
@login_required
def employee_details(request, empid): #empid passed through URL/link
obj_list = Employee.objects.all()
e = Employee.objects.filter(pk=int(empid)).values()[0]
form = EmployeeForm(e)
context_instance=RequestContext(request) #I seem to always need this for {%extend "base.html" %} to work correctly
return render_to_response('employee_create.html', locals(), context_instance,)
#URLconf
(r'^employee/(?P<empid>\d+)/$', employee_details),
# snippets of employee_create.html. The same template used for create and update/edit, may be a source of problems, they do have different views- just render to same template to stay DRY, but could add an additional layer of extend for differences needed between the new and edit requests EDIT: added a 3rd layer of templates to solve this "problem". not shown in code here- easy enough to add another child template
{% extends "base.html" %}
{% block title %}New Entry{% endblock %}
{% block content %}
<div id="employeeform">
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="/newemp/" method="post" class="employeeform">{% csrf_token %} #SEE EDIT
<div class="left_field">
{{ form.employeeid.value }}
{{ form.currentemployee.errors }}
<label for="currentemployee" >Current Employee?</label>
{{ form.currentemployee }}<br/><br/>
{{ form.employer.errors }}
<label for="employer" class="fixedwidth">Employer:</label>
{{ form.employer }}<br/>
{{ form.last_name.errors }}
<label for="last_name" class="fixedwidth">Last Name:</label>
{{ form.last_name }}<br/>
{{ form.facility.errors }} #ManyToMany
<label for="facility" class="fixedwidth">Facility:</label>
{{ form.facility }}<br/><br/>
</div>
<div id="submit"><br/>
<input type="submit" value="Submit">
</div>
</form>
#models.py
class Employee(models.Model):
employeeid = models.AutoField(primary_key=True, verbose_name='Employee ID #')
currentemployee = models.BooleanField(null=False, blank=True, verbose_name='Current Employee?')
employer = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
facility = models.ForeignKey(Facility, null=True, blank=True)
base.html には、上部にヘッダー、左側にメニューがあり、フォームや従業員テーブルなどがすべて拡張された大きな空の div があります。
では、新しいエントリを作成するのではなく、エントリを更新するためにテンプレートのビューや を変更するにはどうすればよいでしょうか? ( また、正しい foriegnkeys を入力するにはどうすればよいですか? (ドロップダウン ボックスには適切なオプションが用意されていますが、元のデータベース エントリに正しい情報が含まれていても、"-----" が選択されています。
さらにファイル/コードを含める必要があるかどうか教えてください。他にも写真がありますが、これ以上リンクしたり、新しいユーザーとして挿入したりすることはできません :< 私は貢献して他の人を助ける必要があります! :D
編集:私はこれにもっと取り組んできましたが、あまり進んでいません。データベース (SQLite3) に保存されている値を選択するためのドロップダウン フィールドをまだ取得できません。しかし、私が理解しようとしている主な問題は、新しいエントリではなく、更新として保存する方法です。save(force_update=True) は、デフォルトの ModelForm 保存パラメータでは機能しません。