私はできるようにしたい:
def update_profile(request, username):
user = Profile.objects.get(user__username=username)
# update subset of profile, eg value_to_company is set in request.POST
# but user (or an arbitrary number of other attributes) is not
profile = ProfileForm(request.POST, instance=user)
if not profile.is_valid():
print profile.errors #user is required
これで私が抱えている問題は、ユーザーが確実に存在し、インスタンスの一部であり、インスタンスデータがバインドされたデータに入らないことを示唆するドキュメントが見つからないことです。しかし、それができると明示的に言っているものも見つかりません。
(残りの部分は、私が自分のやり方で物事を行っている理由の正当化と、解決策のアイデアです。おそらく読む必要はありません。)
なぜ私がフォームを使用しているのかを知りたい場合は、質問に不可欠ではありませんが、次のようなものがあるからです。
class Profile(models.Model):
user = models.ForeignKey(auth.models.User)
value_to_company = models.IntegerField()
class ValueField(forms.Field):
def to_python(self, value):
vals = {'high': 0,
'mid': 1}
return vals[value]
class ProfileForm(forms.ModelForm):
value_to_company = ValueField()
class Meta:
model = Profile
つまり、私はすでに自分のフォームで type api-to-internal-representation-coercion を行っており、それを引き続き使用したいと考えています。
forms.is_valid() ループを再実装して、既に存在するフォームのフィールドをチェックするだけです。
# replaces `if not profile.is_valid()` above:
errors = []
for field in request.POST.iterkeys():
if field in profile.fields:
profile.fields[field].to_python()
if not profile.fields['field'].clean():
errors.append #something
(私はロジックの内部ロジックを実際に見ていないので、それが間違っていることはわかっていますが、その考えはわかります。)