Search code examples
pythondjangodjango-allauth

allauth account edit in django


I am building an application with django and I decided to opt for the allauth authentication. now I have a profile page and I want users to be able to edit their account to Include Bios, Last_name ,first_name and thingx like that and possibly include a display picture but I inplemented some codes and I got an error an I have been stocked at it for a long time. the Error is

error

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

That is my code above with the erro and I just can not get to wrap my head around what the problem is.Any help would be appreciated.


Solution

  • remove 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
    

    in Model meta you can use only models options

    If you want to extend the default django user model, read the auth customizing