Newly added fields not showing in Profile Edit form in Django Oscar?

我们两清 提交于 2019-12-24 17:24:22

问题


I want to extend the User model. I followed the steps mentioned in this doc. I made a new app extended_user whose models.py reads as :

from django.db import models

from oscar.apps.customer.abstract_models import AbstractUser
from django.utils.translation import ugettext_lazy as _


class User(AbstractUser):

    nickname =  models.CharField(_("nick_name"), max_length=50, null=True, blank=True)

    def get_full_name(self):
        full_name = '%s %s' % (self.last_name.upper(), self.first_name)
        return full_name.strip()

In settings.py I mention

AUTH_USER_MODEL = "extended_user.User"

I make and run migrations. In profile view I can see nickname field but in Profile Edit view I don't. What do I have to do to see the newly added field in the Profile Edit form ?


回答1:


I assume that you do not use a separate rofile class. In that case Oscar sets ProfileForm to point to the UserForm class.

That class in turn has a more-or-less hardcoded list of fields. (In reality it says "whichever fields exist out of this list".)

The easiest way to go forward from here is to override customer.forms.ProfileForm with your own class that uses your newly defined User model and the list of fields that better suits your usecase. (Create a your_app.customer.forms module and define a ProfileForm inside.)




回答2:


  1. python3 manage.py oscar_fork_app customer
  2. add your app on your settings :
from oscar import get_core_apps

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.flatpages',
    'widget_tweaks',
    'YOUR_APP',
] + get_core_apps(['customer'])
  1. The last part is to put this content of customer/forms.py:
    • it changes the Model with yours to access new added fields;
    • extends existing UserForm to keep all clean data;
    • change ProfileForm var as Oscar use it for render form of profile to point to your new form.
from oscar.apps.customer.forms import UserForm as CoreUserForm
from user.models import User
from django import forms

from oscar.core.compat import existing_user_fields

class UserForm(CoreUserForm):

    class Meta:
        model = User
        fields = existing_user_fields(['username', 'first_name', 'last_name', 'email'])

ProfileForm = UserForm


来源:https://stackoverflow.com/questions/32268262/newly-added-fields-not-showing-in-profile-edit-form-in-django-oscar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!