我有一个Django模型类,它扩展了django.contrib.auth.models.User
:
class MyModel(models.Model):
user = models.OneToOneField(User, models.CASCADE, unique=True)
bio = models.TextField()
date_of_birth = models.DateField()
并且在这个模型中我制作了一个ModelForm:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ['bio', 'date_of_birth']
如何告诉MyModelForm接受用户的字段,例如username
,first_name
,last_name
?
类Meta字段中的示例我可以添加['bio', 'date_of_birth', 'user']
但这只会为我提供下拉列表,以选择与MyModelForm中的MyModel相关联的人。
执行['bio', 'date_of_birth', 'user.first_name']
会引发异常django.core.exceptions.FieldError: Unknown field(s) (user.first_name) specified for MyModel
编辑:这里是追溯 https://pastebin.com/0T42VKEP
答案 0 :(得分:1)
您可以使用InlinFormset。在这里,我发布了一个可能对您有帮助的代码示例。我已根据您的需要进行了一些修改,所以如果有什么不起作用,请告诉我。
#forms.py
class UserForm(forms.ModelForm):
class Meta:
model = User
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
然后在你的观点中你应该这样做
def profileEdit(request,username):
# querying the User object with pk from url
user = User.objects.get(username=username)
# prepopulate MyModelForm with retrieved user values from above.
user_form = UserForm(instance=user)
MyModelInlineFormset = inlineformset_factory(User, MyModel,can_delete=False, fields="__all__")
formset = MyModelInlineFormset(instance=user)
if request.user.is_authenticated() and request.user.id == user.id:
if request.method == "POST":
user_form = UserForm(request.POST, request.FILES, instance=user)
formset = MyModelInlineFormset(request.POST, request.FILES, instance=user)
if user_form.is_valid():
created_user = user_form.save(commit=False)
formset = MyModelInlineFormset(request.POST, request.FILES, instance=created_user)
if formset.is_valid():
created_user.save()
formset.save()
return HttpResponseRedirect('/')
return render(request, "profile_edit.html", {
'title':'Edit -'+ user.get_full_name(),
"user_form": user_form,
"formset": formset,
})
else:
raise PermissionDenied
并在您的profile_edit.html
中{% extends "layout.html" %}
{% block content %}
{% if messages %}
<div>
{% for message in messages %}
<div class="alert alert-{{ message.tags }}"> <!-- singular -->
<a class="close" data-dismiss="alert">×</a>
{{ message|safe }}
</div>
{% endfor %}
</div>
{% endif %}
<div class="col-xs-8 col-xs-offset-2">
<div class="card">
<div class="card-content">
<h2 class="flow-text">Update your information</h2>
<form action="." method="POST" class="padding" enctype="multipart/form-data">
{% csrf_token %}
{% for field in user_form %}
<div class="form-group">
{{ field.errors }}
<label for="{{ field.label }}" >{{ field.label }}</label>
{{ field }}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text|safe }}</small>
{% endif %}
</div>
{% endfor %}
{{ formset.management_form }}
{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.errors }}
<label for="{{ field.label }}" >{{ field.label }}</label>
{{ field }}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text|safe }}</small>
{% endif %}
</div>
{% endfor %}
{% endfor %}
<input type="submit" class="btn btn-primary" value="Save"></input>
</form>
</div>
</div>
</div>
{% endblock content %}
答案 1 :(得分:1)
通过重写ModelForm的__init__
方法,我找到了自己的解决方案:
class MyModelForm(forms.ModelForm):
first_name = forms.CharField()
class Meta:
model = MyModel
fields = ['bio', 'date_of_birth']
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
my_user = kwargs.get('instance')
first_name = my_user.user.first_name
self.fields['first_name'].initial = first_name #this will show the first name in the html page when i request the instance
当我实例化我的表单时,我只是将MyModel的一个实例作为一个
kwarg:form = MyModelForm(instance=my_model_instance)