我是Django的新手,正在尝试将社交登录选项添加到我正在从事的项目中。我目前正在使用class CountByUserGraph extends React.Component {
this.massPopChart;
constructor() {
super();
this.state = { selectedOption: "A" };
}
componentDidMount() {
this.makeApiCall();
}
makeApiCall = () => {
countByUserGraph(this.state.selectedOption) // API CALL
.then(response => {
// recieves data
this.getChartData(data);
})
.catch(err => console.log(err));
};
getChartData = data => {
let myChart = document.getElementById("myChart0").getContext("2d");
const CountValue = data.map(a => a.countValue);
const CountMonth = data.map(a => a.month);
var data = {
labels: CountMonth,
datasets: [
{
label: "Count",
data: CountValue
}
]
};
var option = {
responsive: true
// Options
};
if(this.massPopChart){
massPopChart.data = data;
//massPopChart.config.data = data; //use this if above code does not work
massPopChart.update();
}
else{
this.massPopChart = new Chart.Line(myChart, { data: data, options: option });
}
};
handleOptionChange = changeEvent => {
this.setState({ selectedOption: changeEvent.target.value }, function() {
this.makeApiCall();
});
};
render() {
return (
<div>
<form className="graph-filter">
<div>
<input
type="radio"
id="A"
name="A"
checked={this.state.selectedOption === "A"}
onClick={this.handleOptionChange}
value="A"
/>
<label htmlFor="A">A</label>
</div>
<div>
<input
type="radio"
id="B"
name="B"
checked={this.state.selectedOption === "B"}
onClick={this.handleOptionChange}
value="B"
/>
<label htmlFor="B">B</label>
</div>
</form>
<canvas id="myChart0"></canvas>
</div>
);
}
}
来实现相同目的。目前,我仅将Google登录名集成到我的项目中。一切正常。我已经为我的项目创建了一个自定义用户模型,并使用电子邮件作为用户名。当用户尝试使用我提供的“注册模板”进行注册时,可以毫无问题地从数据库中删除该用户。但是,如果我尝试从管理面板中删除使用django-allauth的Google选项登录的任何用户,则会收到Django-Allauth
错误。我重置所有内容的唯一方法是简单地删除模型并重做所有内容。下面是我的模型的样子。我正在使用Django 3.0和最新版本的Django-Allauth。我不确定如何解决这个问题。任何帮助,将不胜感激。
models.py
__str__ returned non-string (type CustomUser)
Settings.py
class CustomUser(AbstractBaseUser):
email = models.EmailField(unique=True, max_length=100)
first_name = models.CharField(max_length=35)
last_name = models.CharField(max_length=35)
mobile = models.CharField(max_length=11, blank=True, default='')
city = models.CharField(max_length=35, blank=True, default='')
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
objects = CustomUserManager()
def __str__(self):
return self.email
def get_full_name(self):
if self.first_name and self.last_name:
return self.first_name + " " + self.last_name
def get_short_name(self):
if self.first_name:
return self.first_name
def has_perm(self, perm, obj=None):
"""
Does the user have a specific permission?
"""
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"""
Does the user have permissions to view the app `app_label`?
"""
# Simplest possible answer: Yes, always
return True
def get_absolute_url(self):
return reverse('users-profile', args=[str(self.id)])
urls.py
# Application definition
INSTALLED_APPS = [
# Default Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites', # Added
# User Apps
'users.apps.UsersConfig',
'crispy_forms',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
]
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend', # Added
)
# Required for Django-Allauth
SITE_ID = 1
# Required for Django-Allauth
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
以下是错误回溯:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('users.urls')),
path('accounts/', include('allauth.urls')),
]
答案 0 :(得分:0)
AbstractBaseUser
有一个__str__
方法,该方法返回模型中USERNAME_FIELD
上指定的字段。您可以在Github上查看源代码。
因此,在指定自定义USERNAME_FIELD
之后,您可以删除自己的__str__
方法:
class CustomUser(AbstractBaseUser):
email = models.EmailField(unique=True, max_length=100)
first_name = models.CharField(max_length=35)
last_name = models.CharField(max_length=35)
mobile = models.CharField(max_length=11, blank=True, default='')
city = models.CharField(max_length=35, blank=True, default='')
USERNAME_FIELD = 'email'