Django: New field not showing up in form after adding it to modelCan't add field to ModelForm at __init__Django south migration error with unique field in postgresql databaseForm with fields from more than one modeldisplay parent field on django formscustom user registration form in djangofilter json data from Django modelDjango allauth saving custom user profile fields with signup formHow to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldHow to implement update_or_create inside create method of ModelSerializer
How to find better food in airports
Blogging in LaTeX
The 7-numbers crossword
Polarity of gas discharge tubes?
Why don't "echo -e" commands seem to produce the right output?
What is the motivation behind designing a control stick that does not move?
D Scale Question
Can authors email you PDFs of their textbook for free?
An alternative to "two column" geometry proofs
Heuristic argument for the Riemann Hypothesis
Was there an original and definitive use of alternate dimensions/realities in fiction?
Divide Numbers by 0
Table alignment (make the content centre)
meaning of "educating the ice"?
Is torque as fundamental a concept as force?
Calculate Landau's function
How do I get my neighbour to stop disturbing with loud music?
Can my UK debt be collected because I have to return to US?
Is there anything in the universe that cannot be compressed?
Should we run PBKDF2 for every plaintext to be protected or should we run PBKDF2 only once?
Can a human variant take proficiency in initiative?
Can users with the same $HOME have separate bash histories?
Ways you can end up paying interest on a credit card if you pay the full amount back in due time
extending lines in 3d graph
Django: New field not showing up in form after adding it to model
Can't add field to ModelForm at __init__Django south migration error with unique field in postgresql databaseForm with fields from more than one modeldisplay parent field on django formscustom user registration form in djangofilter json data from Django modelDjango allauth saving custom user profile fields with signup formHow to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldHow to implement update_or_create inside create method of ModelSerializer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
After adding a new field to my model, it doesn't show up in the form.
I have been working on this django app and have extended the already existing User model with my own UserProfile model with extra fields. Recently, I have decided to add an extra field to the UserProfile model (profile_type). I added it to the model and included it in its corresponding form, and ran makemigrations and migrations. I then tried inserting an initial value to this field in my views and found out that it is not showing up in the form through print(), however, all the other fields are.
My UserProfile model
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, related_name='userprofile')
gender = models.CharField(max_length = 10)
city = models.CharField(max_length = 45)
country = models.CharField(max_length = 45)
birthdate = models.DateField(null=True)
phone_number = models.CharField(max_length = 15)
profile_type = models.CharField(max_length = 6)
@receiver(post_save, sender=User)
def save_profile(sender, instance, created, **kwargs):
if created:
profile = UserProfile(user=instance)
profile.save()
My UserProfile forms (UserProfileForm contains the fields necessary for sign up, AdditionalUserProfileForm is the rest)
class UserProfileForm(UserCreationForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
def save(self, commit=True):
user = super(UserProfileForm, self).save(commit=True)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return user
class AdditionalUserProfileForm(forms.ModelForm):
profile_type = forms.CharField(max_length=6)
class Meta:
model = UserProfile
fields = ('gender', 'city', 'country', 'birthdate', 'phone_number', 'profile_type')
My Sign Up view (I want to add the value to profile_type manually upon instantiation
@transaction.atomic
def signup_view(request):
if request.method == 'POST':
user_profile_form = UserProfileForm(request.POST)
initial =
'profile_type': 'user'
additional_user_profile_form = AdditionalUserProfileForm(request.POST, initial=initial)
valid = user_profile_form.is_valid() * additional_user_profile_form.is_valid()
if valid:
user = user_profile_form.save()
for field in ['gender', 'city', 'country', 'birthdate', 'phone_number']:
setattr(user.userprofile, field,
additional_user_profile_form.cleaned_data.get(field))
user.userprofile.save()
return redirect('login')
else:
user_profile_form = UserProfileForm()
additional_user_profile_form = AdditionalUserProfileForm()
context =
'user_profile_form': user_profile_form,
'additional_user_profile_form': additional_user_profile_form,
return render(request, 'registration/signup.html', context)
Printing out the form fields in the terminal doesn't show that profile_type is one of them. I saw on some thread that it's a bug but the fix did not work for me. It was something about changing the get_fieldsets function in contrib/admin/options.py. Hope this gives you a hint. Thank you!
python django forms model
add a comment |
After adding a new field to my model, it doesn't show up in the form.
I have been working on this django app and have extended the already existing User model with my own UserProfile model with extra fields. Recently, I have decided to add an extra field to the UserProfile model (profile_type). I added it to the model and included it in its corresponding form, and ran makemigrations and migrations. I then tried inserting an initial value to this field in my views and found out that it is not showing up in the form through print(), however, all the other fields are.
My UserProfile model
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, related_name='userprofile')
gender = models.CharField(max_length = 10)
city = models.CharField(max_length = 45)
country = models.CharField(max_length = 45)
birthdate = models.DateField(null=True)
phone_number = models.CharField(max_length = 15)
profile_type = models.CharField(max_length = 6)
@receiver(post_save, sender=User)
def save_profile(sender, instance, created, **kwargs):
if created:
profile = UserProfile(user=instance)
profile.save()
My UserProfile forms (UserProfileForm contains the fields necessary for sign up, AdditionalUserProfileForm is the rest)
class UserProfileForm(UserCreationForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
def save(self, commit=True):
user = super(UserProfileForm, self).save(commit=True)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return user
class AdditionalUserProfileForm(forms.ModelForm):
profile_type = forms.CharField(max_length=6)
class Meta:
model = UserProfile
fields = ('gender', 'city', 'country', 'birthdate', 'phone_number', 'profile_type')
My Sign Up view (I want to add the value to profile_type manually upon instantiation
@transaction.atomic
def signup_view(request):
if request.method == 'POST':
user_profile_form = UserProfileForm(request.POST)
initial =
'profile_type': 'user'
additional_user_profile_form = AdditionalUserProfileForm(request.POST, initial=initial)
valid = user_profile_form.is_valid() * additional_user_profile_form.is_valid()
if valid:
user = user_profile_form.save()
for field in ['gender', 'city', 'country', 'birthdate', 'phone_number']:
setattr(user.userprofile, field,
additional_user_profile_form.cleaned_data.get(field))
user.userprofile.save()
return redirect('login')
else:
user_profile_form = UserProfileForm()
additional_user_profile_form = AdditionalUserProfileForm()
context =
'user_profile_form': user_profile_form,
'additional_user_profile_form': additional_user_profile_form,
return render(request, 'registration/signup.html', context)
Printing out the form fields in the terminal doesn't show that profile_type is one of them. I saw on some thread that it's a bug but the fix did not work for me. It was something about changing the get_fieldsets function in contrib/admin/options.py. Hope this gives you a hint. Thank you!
python django forms model
Did you run makemigrations and migrate?
– user9727749
Mar 28 at 2:49
Yes I have, and it turned out I had to add the field to the template, otherwise it wouldn't be considered as part of the form, even if it is included in the form class, my bad.
– Hsen Shamseddine
Mar 28 at 2:56
add a comment |
After adding a new field to my model, it doesn't show up in the form.
I have been working on this django app and have extended the already existing User model with my own UserProfile model with extra fields. Recently, I have decided to add an extra field to the UserProfile model (profile_type). I added it to the model and included it in its corresponding form, and ran makemigrations and migrations. I then tried inserting an initial value to this field in my views and found out that it is not showing up in the form through print(), however, all the other fields are.
My UserProfile model
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, related_name='userprofile')
gender = models.CharField(max_length = 10)
city = models.CharField(max_length = 45)
country = models.CharField(max_length = 45)
birthdate = models.DateField(null=True)
phone_number = models.CharField(max_length = 15)
profile_type = models.CharField(max_length = 6)
@receiver(post_save, sender=User)
def save_profile(sender, instance, created, **kwargs):
if created:
profile = UserProfile(user=instance)
profile.save()
My UserProfile forms (UserProfileForm contains the fields necessary for sign up, AdditionalUserProfileForm is the rest)
class UserProfileForm(UserCreationForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
def save(self, commit=True):
user = super(UserProfileForm, self).save(commit=True)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return user
class AdditionalUserProfileForm(forms.ModelForm):
profile_type = forms.CharField(max_length=6)
class Meta:
model = UserProfile
fields = ('gender', 'city', 'country', 'birthdate', 'phone_number', 'profile_type')
My Sign Up view (I want to add the value to profile_type manually upon instantiation
@transaction.atomic
def signup_view(request):
if request.method == 'POST':
user_profile_form = UserProfileForm(request.POST)
initial =
'profile_type': 'user'
additional_user_profile_form = AdditionalUserProfileForm(request.POST, initial=initial)
valid = user_profile_form.is_valid() * additional_user_profile_form.is_valid()
if valid:
user = user_profile_form.save()
for field in ['gender', 'city', 'country', 'birthdate', 'phone_number']:
setattr(user.userprofile, field,
additional_user_profile_form.cleaned_data.get(field))
user.userprofile.save()
return redirect('login')
else:
user_profile_form = UserProfileForm()
additional_user_profile_form = AdditionalUserProfileForm()
context =
'user_profile_form': user_profile_form,
'additional_user_profile_form': additional_user_profile_form,
return render(request, 'registration/signup.html', context)
Printing out the form fields in the terminal doesn't show that profile_type is one of them. I saw on some thread that it's a bug but the fix did not work for me. It was something about changing the get_fieldsets function in contrib/admin/options.py. Hope this gives you a hint. Thank you!
python django forms model
After adding a new field to my model, it doesn't show up in the form.
I have been working on this django app and have extended the already existing User model with my own UserProfile model with extra fields. Recently, I have decided to add an extra field to the UserProfile model (profile_type). I added it to the model and included it in its corresponding form, and ran makemigrations and migrations. I then tried inserting an initial value to this field in my views and found out that it is not showing up in the form through print(), however, all the other fields are.
My UserProfile model
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, related_name='userprofile')
gender = models.CharField(max_length = 10)
city = models.CharField(max_length = 45)
country = models.CharField(max_length = 45)
birthdate = models.DateField(null=True)
phone_number = models.CharField(max_length = 15)
profile_type = models.CharField(max_length = 6)
@receiver(post_save, sender=User)
def save_profile(sender, instance, created, **kwargs):
if created:
profile = UserProfile(user=instance)
profile.save()
My UserProfile forms (UserProfileForm contains the fields necessary for sign up, AdditionalUserProfileForm is the rest)
class UserProfileForm(UserCreationForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
def save(self, commit=True):
user = super(UserProfileForm, self).save(commit=True)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return user
class AdditionalUserProfileForm(forms.ModelForm):
profile_type = forms.CharField(max_length=6)
class Meta:
model = UserProfile
fields = ('gender', 'city', 'country', 'birthdate', 'phone_number', 'profile_type')
My Sign Up view (I want to add the value to profile_type manually upon instantiation
@transaction.atomic
def signup_view(request):
if request.method == 'POST':
user_profile_form = UserProfileForm(request.POST)
initial =
'profile_type': 'user'
additional_user_profile_form = AdditionalUserProfileForm(request.POST, initial=initial)
valid = user_profile_form.is_valid() * additional_user_profile_form.is_valid()
if valid:
user = user_profile_form.save()
for field in ['gender', 'city', 'country', 'birthdate', 'phone_number']:
setattr(user.userprofile, field,
additional_user_profile_form.cleaned_data.get(field))
user.userprofile.save()
return redirect('login')
else:
user_profile_form = UserProfileForm()
additional_user_profile_form = AdditionalUserProfileForm()
context =
'user_profile_form': user_profile_form,
'additional_user_profile_form': additional_user_profile_form,
return render(request, 'registration/signup.html', context)
Printing out the form fields in the terminal doesn't show that profile_type is one of them. I saw on some thread that it's a bug but the fix did not work for me. It was something about changing the get_fieldsets function in contrib/admin/options.py. Hope this gives you a hint. Thank you!
python django forms model
python django forms model
asked Mar 28 at 1:03
Hsen ShamseddineHsen Shamseddine
161 silver badge3 bronze badges
161 silver badge3 bronze badges
Did you run makemigrations and migrate?
– user9727749
Mar 28 at 2:49
Yes I have, and it turned out I had to add the field to the template, otherwise it wouldn't be considered as part of the form, even if it is included in the form class, my bad.
– Hsen Shamseddine
Mar 28 at 2:56
add a comment |
Did you run makemigrations and migrate?
– user9727749
Mar 28 at 2:49
Yes I have, and it turned out I had to add the field to the template, otherwise it wouldn't be considered as part of the form, even if it is included in the form class, my bad.
– Hsen Shamseddine
Mar 28 at 2:56
Did you run makemigrations and migrate?
– user9727749
Mar 28 at 2:49
Did you run makemigrations and migrate?
– user9727749
Mar 28 at 2:49
Yes I have, and it turned out I had to add the field to the template, otherwise it wouldn't be considered as part of the form, even if it is included in the form class, my bad.
– Hsen Shamseddine
Mar 28 at 2:56
Yes I have, and it turned out I had to add the field to the template, otherwise it wouldn't be considered as part of the form, even if it is included in the form class, my bad.
– Hsen Shamseddine
Mar 28 at 2:56
add a comment |
0
active
oldest
votes
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%2f55388689%2fdjango-new-field-not-showing-up-in-form-after-adding-it-to-model%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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%2f55388689%2fdjango-new-field-not-showing-up-in-form-after-adding-it-to-model%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
Did you run makemigrations and migrate?
– user9727749
Mar 28 at 2:49
Yes I have, and it turned out I had to add the field to the template, otherwise it wouldn't be considered as part of the form, even if it is included in the form class, my bad.
– Hsen Shamseddine
Mar 28 at 2:56