我正在使用django构建应用程序,我决定选择allauth身份验证。现在我有一个个人资料页面,我希望用户能够编辑他们的帐户,包括Bios,Last_name,first_name和thingx,并且可能包含一个显示图片,但我添加了一些代码,我收到了一个错误,我已经存放了它很长一段时间。错误是
错误
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/management/__init__.py", line 337, in execute
django.setup()
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Olar/Desktop/arbithub/src/profiles/models.py", line 27, in <module>
class Profile(models.Model):
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/db/models/base.py", line 124, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/db/models/base.py", line 331, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/db/models/options.py", line 206, in contribute_to_class
raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
TypeError: 'class Meta' got invalid attribute(s): fields,model
model.py
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class Profile(models.Model):
username = models.CharField(max_length=125)
email = models.EmailField(max_length=125)
first_name = models.CharField(max_length=125)
last_name = models.CharField(max_length=125)
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name')
def __str__(self):
return self.username
Forms.py
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from profiles.models import Profile
class UpdateProfile(forms.ModelForm):
username = forms.CharField(required=True)
email = forms.EmailField(required=True)
first_name = forms.CharField(required=False)
last_name = forms.CharField(required=False)
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name')
def clean_email(self):
username = self.cleaned_data.get('username')
email = self.cleaned_data.get('email')
if email and User.objects.filter(email=email).exclude(username=username).count():
raise forms.ValidationError('This email address is already in use. Please supply a different email address.')
return email
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
这是我上面的代码与错误,我只是无法解决问题是什么。任何帮助将不胜感激。
答案 0 :(得分:3)
此代码
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name')
不属于您的模型。因此,model
和fields
都是模型Meta
类的无效关键字参数。
编辑回复评论:
如果您想添加更多字段,可以在模型中添加它们:
class MyModel(models.Model):
my_new_field = models.IntegerField()
然后,在您的模型表单中,将my_new_field
添加到fields
参数中。请注意,除非您想要执行超出模型提供的功能,否则只需将其添加到tuple
字段即可。换句话说,您不需要添加:
my_new_field = forms.IntegerField()
ModelForm
会为您神奇地处理这个问题,如文档中所示:
https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#modelform
这是使用模型表单和模型视图集等优点。它通过重用模型的模式,推断字段和字段类型来保持代码DRY。
我希望能够解决问题。
答案 1 :(得分:1)
删除attrs
class Profile(models.Model):
# ... your code here
class Meta: # And this line need to remove
model = User # And this line need to remove
fields = ('username', 'email', 'first_name', 'last_name') # this line need to remove
在Model meta中,您只能使用models options
如果您想扩展默认的django用户模型,请阅读auth customizing