0

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.

2
  • Is your view returning a response? Can you share the rest of your view? If you have ATOMIC_REQUESTS set to True and you don't return a valid response the form won't save Commented Jan 24, 2021 at 4:21
  • I got it. from account_form = CompanyAccountForm(request.POST, instance=request.user) to account_form = CompanyAccountForm(request.POST, instance=company ) But there is another problem. My link to Account was removed. Commented Jan 24, 2021 at 4:30

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

You can only use exclude instead of using both fields and exclude
Oh. did not know that. Will update rn. Thank you for the advice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.