django.core.exceptions.FieldError: Unknown field(s) (username) specified for Employee
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to extend the user model
so I created a new model called employee with foreignkey to user model
from django.db import models
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=200)
and tried to create a form for the signup
from django.contrib.auth.forms import UserCreationForm
from employee.models import Employee
class EmployeeForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Employee
fields = UserCreationForm.Meta.fields + ('department',)
These are the only changes I made I am getting the following error:
File "/home/sugumar/python/django/project1/project1/urls.py", line
18, in
from employee.views import signup File "/home/sugumar/python/django/project1/employee/views.py", line 2, in
from .forms import EmployeeForm File "/home/sugumar/python/django/project1/employee/forms.py", line 4, in
class EmployeeForm(UserCreationForm): File "/home/sugumar/.local/share/virtualenvs/project1-j0yhUYNK/lib/python3.5/site-packages/django/forms/models.py",
line 266, in new
raise FieldError(message) django.core.exceptions.FieldError: Unknown field(s) (username) specified for Employee
python django
add a comment |
I am trying to extend the user model
so I created a new model called employee with foreignkey to user model
from django.db import models
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=200)
and tried to create a form for the signup
from django.contrib.auth.forms import UserCreationForm
from employee.models import Employee
class EmployeeForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Employee
fields = UserCreationForm.Meta.fields + ('department',)
These are the only changes I made I am getting the following error:
File "/home/sugumar/python/django/project1/project1/urls.py", line
18, in
from employee.views import signup File "/home/sugumar/python/django/project1/employee/views.py", line 2, in
from .forms import EmployeeForm File "/home/sugumar/python/django/project1/employee/forms.py", line 4, in
class EmployeeForm(UserCreationForm): File "/home/sugumar/.local/share/virtualenvs/project1-j0yhUYNK/lib/python3.5/site-packages/django/forms/models.py",
line 266, in new
raise FieldError(message) django.core.exceptions.FieldError: Unknown field(s) (username) specified for Employee
python django
You can not inherit fields like that, anEmployeehas nousername. I think the smallest change is to use two forms, and thus render it as one, and then handle both forms, and construct aUserandEmployeeobject.
– Willem Van Onsem
Jan 3 at 22:02
@Willem Van Onsem, so there is no way to add custom fields to existing forms, right?
– Sugumar Venkatesan
Jan 3 at 22:06
there is, you can simply add fields to your form, but that will not make a "binding" to the model fields (which is after all the convenience aModelFormoffers most people are interested in).
– Willem Van Onsem
Jan 3 at 22:06
add a comment |
I am trying to extend the user model
so I created a new model called employee with foreignkey to user model
from django.db import models
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=200)
and tried to create a form for the signup
from django.contrib.auth.forms import UserCreationForm
from employee.models import Employee
class EmployeeForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Employee
fields = UserCreationForm.Meta.fields + ('department',)
These are the only changes I made I am getting the following error:
File "/home/sugumar/python/django/project1/project1/urls.py", line
18, in
from employee.views import signup File "/home/sugumar/python/django/project1/employee/views.py", line 2, in
from .forms import EmployeeForm File "/home/sugumar/python/django/project1/employee/forms.py", line 4, in
class EmployeeForm(UserCreationForm): File "/home/sugumar/.local/share/virtualenvs/project1-j0yhUYNK/lib/python3.5/site-packages/django/forms/models.py",
line 266, in new
raise FieldError(message) django.core.exceptions.FieldError: Unknown field(s) (username) specified for Employee
python django
I am trying to extend the user model
so I created a new model called employee with foreignkey to user model
from django.db import models
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=200)
and tried to create a form for the signup
from django.contrib.auth.forms import UserCreationForm
from employee.models import Employee
class EmployeeForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Employee
fields = UserCreationForm.Meta.fields + ('department',)
These are the only changes I made I am getting the following error:
File "/home/sugumar/python/django/project1/project1/urls.py", line
18, in
from employee.views import signup File "/home/sugumar/python/django/project1/employee/views.py", line 2, in
from .forms import EmployeeForm File "/home/sugumar/python/django/project1/employee/forms.py", line 4, in
class EmployeeForm(UserCreationForm): File "/home/sugumar/.local/share/virtualenvs/project1-j0yhUYNK/lib/python3.5/site-packages/django/forms/models.py",
line 266, in new
raise FieldError(message) django.core.exceptions.FieldError: Unknown field(s) (username) specified for Employee
python django
python django
asked Jan 3 at 21:57
Sugumar VenkatesanSugumar Venkatesan
2,09221335
2,09221335
You can not inherit fields like that, anEmployeehas nousername. I think the smallest change is to use two forms, and thus render it as one, and then handle both forms, and construct aUserandEmployeeobject.
– Willem Van Onsem
Jan 3 at 22:02
@Willem Van Onsem, so there is no way to add custom fields to existing forms, right?
– Sugumar Venkatesan
Jan 3 at 22:06
there is, you can simply add fields to your form, but that will not make a "binding" to the model fields (which is after all the convenience aModelFormoffers most people are interested in).
– Willem Van Onsem
Jan 3 at 22:06
add a comment |
You can not inherit fields like that, anEmployeehas nousername. I think the smallest change is to use two forms, and thus render it as one, and then handle both forms, and construct aUserandEmployeeobject.
– Willem Van Onsem
Jan 3 at 22:02
@Willem Van Onsem, so there is no way to add custom fields to existing forms, right?
– Sugumar Venkatesan
Jan 3 at 22:06
there is, you can simply add fields to your form, but that will not make a "binding" to the model fields (which is after all the convenience aModelFormoffers most people are interested in).
– Willem Van Onsem
Jan 3 at 22:06
You can not inherit fields like that, an
Employee has no username. I think the smallest change is to use two forms, and thus render it as one, and then handle both forms, and construct a User and Employee object.– Willem Van Onsem
Jan 3 at 22:02
You can not inherit fields like that, an
Employee has no username. I think the smallest change is to use two forms, and thus render it as one, and then handle both forms, and construct a User and Employee object.– Willem Van Onsem
Jan 3 at 22:02
@Willem Van Onsem, so there is no way to add custom fields to existing forms, right?
– Sugumar Venkatesan
Jan 3 at 22:06
@Willem Van Onsem, so there is no way to add custom fields to existing forms, right?
– Sugumar Venkatesan
Jan 3 at 22:06
there is, you can simply add fields to your form, but that will not make a "binding" to the model fields (which is after all the convenience a
ModelForm offers most people are interested in).– Willem Van Onsem
Jan 3 at 22:06
there is, you can simply add fields to your form, but that will not make a "binding" to the model fields (which is after all the convenience a
ModelForm offers most people are interested in).– Willem Van Onsem
Jan 3 at 22:06
add a comment |
1 Answer
1
active
oldest
votes
I am trying to extend the user model, so I created a new model called employee with foreignkey to user model.
By using a OneToOneField, one can indeed extend the user system, but you can not simply use this to handle both models in the same Form, and thus construct two objects at once.
What you here basically construct is a ModelForm on the Employee model, but here you have constructed a Meta class with extra fields, but these fields are not related to model fields on the Employee object, hence the error.
Probably it is better to simply construct two forms, so the Employee-form looks like:
# app/forms.py
from django.forms import ModelForm
class EmployeeForm(ModelForm):
class Meta:
model = Employee
fields = ('department',)and then create a view like:
# app/views.py
from django.contrib.auth.forms import UserCreationForm
from app.forms import EmployeeForm
def create_user(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
employee_form = EmployeeForm(request.POST)
if user_form.is_valid() and employee_form.is_valid():
user = user_form.save()
employee = employee_form.save(commit=False)
employee.user = user
employee.save()
return redirect('...')
else:
user_form = UserCreationForm()
employee_form = EmployeeForm()
return render(
request,
'app/my_template.html',
{'user_form': user_form, 'employee_form': employee_form}
)In the view we will thus, given both forms are valid, create a User and Employee object and link the employee to the user. We furthermore render the template with two forms.
In the template, we render the two forms in the same <form> tag:
<!-- app/templates/app/my_template.html -->
<form action="{% url 'app:create_user' %}" method="post">
{% csrf_token %}
{{ user_form }}
{{ employee_form }}
</form>Where 'app:create_user' is the name of the URL that points to the view defined above.
Awesome, Thank You.
– Sugumar Venkatesan
Jan 3 at 22:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030372%2fdjango-core-exceptions-fielderror-unknown-fields-username-specified-for-emp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am trying to extend the user model, so I created a new model called employee with foreignkey to user model.
By using a OneToOneField, one can indeed extend the user system, but you can not simply use this to handle both models in the same Form, and thus construct two objects at once.
What you here basically construct is a ModelForm on the Employee model, but here you have constructed a Meta class with extra fields, but these fields are not related to model fields on the Employee object, hence the error.
Probably it is better to simply construct two forms, so the Employee-form looks like:
# app/forms.py
from django.forms import ModelForm
class EmployeeForm(ModelForm):
class Meta:
model = Employee
fields = ('department',)and then create a view like:
# app/views.py
from django.contrib.auth.forms import UserCreationForm
from app.forms import EmployeeForm
def create_user(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
employee_form = EmployeeForm(request.POST)
if user_form.is_valid() and employee_form.is_valid():
user = user_form.save()
employee = employee_form.save(commit=False)
employee.user = user
employee.save()
return redirect('...')
else:
user_form = UserCreationForm()
employee_form = EmployeeForm()
return render(
request,
'app/my_template.html',
{'user_form': user_form, 'employee_form': employee_form}
)In the view we will thus, given both forms are valid, create a User and Employee object and link the employee to the user. We furthermore render the template with two forms.
In the template, we render the two forms in the same <form> tag:
<!-- app/templates/app/my_template.html -->
<form action="{% url 'app:create_user' %}" method="post">
{% csrf_token %}
{{ user_form }}
{{ employee_form }}
</form>Where 'app:create_user' is the name of the URL that points to the view defined above.
Awesome, Thank You.
– Sugumar Venkatesan
Jan 3 at 22:27
add a comment |
I am trying to extend the user model, so I created a new model called employee with foreignkey to user model.
By using a OneToOneField, one can indeed extend the user system, but you can not simply use this to handle both models in the same Form, and thus construct two objects at once.
What you here basically construct is a ModelForm on the Employee model, but here you have constructed a Meta class with extra fields, but these fields are not related to model fields on the Employee object, hence the error.
Probably it is better to simply construct two forms, so the Employee-form looks like:
# app/forms.py
from django.forms import ModelForm
class EmployeeForm(ModelForm):
class Meta:
model = Employee
fields = ('department',)and then create a view like:
# app/views.py
from django.contrib.auth.forms import UserCreationForm
from app.forms import EmployeeForm
def create_user(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
employee_form = EmployeeForm(request.POST)
if user_form.is_valid() and employee_form.is_valid():
user = user_form.save()
employee = employee_form.save(commit=False)
employee.user = user
employee.save()
return redirect('...')
else:
user_form = UserCreationForm()
employee_form = EmployeeForm()
return render(
request,
'app/my_template.html',
{'user_form': user_form, 'employee_form': employee_form}
)In the view we will thus, given both forms are valid, create a User and Employee object and link the employee to the user. We furthermore render the template with two forms.
In the template, we render the two forms in the same <form> tag:
<!-- app/templates/app/my_template.html -->
<form action="{% url 'app:create_user' %}" method="post">
{% csrf_token %}
{{ user_form }}
{{ employee_form }}
</form>Where 'app:create_user' is the name of the URL that points to the view defined above.
Awesome, Thank You.
– Sugumar Venkatesan
Jan 3 at 22:27
add a comment |
I am trying to extend the user model, so I created a new model called employee with foreignkey to user model.
By using a OneToOneField, one can indeed extend the user system, but you can not simply use this to handle both models in the same Form, and thus construct two objects at once.
What you here basically construct is a ModelForm on the Employee model, but here you have constructed a Meta class with extra fields, but these fields are not related to model fields on the Employee object, hence the error.
Probably it is better to simply construct two forms, so the Employee-form looks like:
# app/forms.py
from django.forms import ModelForm
class EmployeeForm(ModelForm):
class Meta:
model = Employee
fields = ('department',)and then create a view like:
# app/views.py
from django.contrib.auth.forms import UserCreationForm
from app.forms import EmployeeForm
def create_user(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
employee_form = EmployeeForm(request.POST)
if user_form.is_valid() and employee_form.is_valid():
user = user_form.save()
employee = employee_form.save(commit=False)
employee.user = user
employee.save()
return redirect('...')
else:
user_form = UserCreationForm()
employee_form = EmployeeForm()
return render(
request,
'app/my_template.html',
{'user_form': user_form, 'employee_form': employee_form}
)In the view we will thus, given both forms are valid, create a User and Employee object and link the employee to the user. We furthermore render the template with two forms.
In the template, we render the two forms in the same <form> tag:
<!-- app/templates/app/my_template.html -->
<form action="{% url 'app:create_user' %}" method="post">
{% csrf_token %}
{{ user_form }}
{{ employee_form }}
</form>Where 'app:create_user' is the name of the URL that points to the view defined above.
I am trying to extend the user model, so I created a new model called employee with foreignkey to user model.
By using a OneToOneField, one can indeed extend the user system, but you can not simply use this to handle both models in the same Form, and thus construct two objects at once.
What you here basically construct is a ModelForm on the Employee model, but here you have constructed a Meta class with extra fields, but these fields are not related to model fields on the Employee object, hence the error.
Probably it is better to simply construct two forms, so the Employee-form looks like:
# app/forms.py
from django.forms import ModelForm
class EmployeeForm(ModelForm):
class Meta:
model = Employee
fields = ('department',)and then create a view like:
# app/views.py
from django.contrib.auth.forms import UserCreationForm
from app.forms import EmployeeForm
def create_user(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
employee_form = EmployeeForm(request.POST)
if user_form.is_valid() and employee_form.is_valid():
user = user_form.save()
employee = employee_form.save(commit=False)
employee.user = user
employee.save()
return redirect('...')
else:
user_form = UserCreationForm()
employee_form = EmployeeForm()
return render(
request,
'app/my_template.html',
{'user_form': user_form, 'employee_form': employee_form}
)In the view we will thus, given both forms are valid, create a User and Employee object and link the employee to the user. We furthermore render the template with two forms.
In the template, we render the two forms in the same <form> tag:
<!-- app/templates/app/my_template.html -->
<form action="{% url 'app:create_user' %}" method="post">
{% csrf_token %}
{{ user_form }}
{{ employee_form }}
</form>Where 'app:create_user' is the name of the URL that points to the view defined above.
edited Jan 4 at 12:09
answered Jan 3 at 22:20
Willem Van OnsemWillem Van Onsem
151k16151238
151k16151238
Awesome, Thank You.
– Sugumar Venkatesan
Jan 3 at 22:27
add a comment |
Awesome, Thank You.
– Sugumar Venkatesan
Jan 3 at 22:27
Awesome, Thank You.
– Sugumar Venkatesan
Jan 3 at 22:27
Awesome, Thank You.
– Sugumar Venkatesan
Jan 3 at 22:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030372%2fdjango-core-exceptions-fielderror-unknown-fields-username-specified-for-emp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can not inherit fields like that, an
Employeehas nousername. I think the smallest change is to use two forms, and thus render it as one, and then handle both forms, and construct aUserandEmployeeobject.– Willem Van Onsem
Jan 3 at 22:02
@Willem Van Onsem, so there is no way to add custom fields to existing forms, right?
– Sugumar Venkatesan
Jan 3 at 22:06
there is, you can simply add fields to your form, but that will not make a "binding" to the model fields (which is after all the convenience a
ModelFormoffers most people are interested in).– Willem Van Onsem
Jan 3 at 22:06