Reputation: 449
Good day SO.
I want to ask something basic. I tried on my end to update my data from html to save to database but to no avail. My Model structure is Account extends to AbstractBaseUser, BaseUserManager
and CompanyAccount links to Account by OneToOneField
. I can say that my Model should have no problem since I can Register Account and Company Account without any problems.
EDIT
Now I can save my CompanyAccount but the link to Account was removed. Still trying to check which is the problem
views.py
@login_required(login_url='/login/')
def user_my_page(request, *args, **kwargs):
context = {}
context['form_type'] = 'update'
context['account_type'] = request.user.account_type
if request.POST:
if request.user.account_type == 1:
company = CompanyAccount.objects.get(account=request.user)
account_form = CompanyAccountForm(request.POST, instance=company)
if account_form.is_valid():
account_form.save()
print(request.POST)
print('SAVE')
else:
print('ERROR')
forms.py
class AccountCreationForm(UserCreationForm):
accuser_id = forms.CharField(max_length=10, help_text="Required")
class Meta:
model = Account
fields = ("accuser_id", "password1", "password2")
class CompanyAccountForm(forms.ModelForm):
class Meta:
model = CompanyAccount
fields = "__all__"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['company_name'].widget.attrs.update({'class': 'content-input'})
self.fields['company_name_kana'].widget.attrs.update({'class': 'content-input'})
print(request.POST)
<QueryDict: {'csrfmiddlewaretoken': ['6zrwt68PNiJKrgZcKanDcVJkqAtogbQbNk2wHwjOzg7ybfq3Lyei9ZqdbmAJcYrV'], 'company_name': ['TESTING'], ....etc
This part here, it does print SAVE on my terminal but it does not save to the database.
Upvotes: 0
Views: 37
Reputation: 449
I got it. Created a new Form for update then excluded the Account.
class CompanyAccountUpdateForm(forms.ModelForm):
class Meta:
model = CompanyAccount
fields = "__all__"
exclude = ('account',)
Then used the new Form to my update view
Upvotes: 1