0

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.

King
  • 1,885
  • 3
  • 27
  • 84

3 Answers3

3

This code

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

does not belong in your model. Hence, both model and fields are invalid keyword arguments for your model's Meta class.

EDIT to respond to comments:

If you want to add more fields, you would add them on your model:

class MyModel(models.Model):
    my_new_field = models.IntegerField()

Then, in your model form, add my_new_field into the fields parameter. Note that unless you want to do something beyond what your model is providing, you only need to add this to the tuple of fields. In other words, you do not need to add:

my_new_field = forms.IntegerField()

ModelForm handles this magically for you, as shown in the docs:

https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#modelform

This is the advantage of using things like model forms and model viewsets. It keeps your code DRY by re-using the schema of your model, inferring the fields and field types.

I hope that clears things up.

MrName
  • 2,363
  • 17
  • 31
  • Ok it works now but if I want to add more fields like Bios and picture upload option . Do I just add the field into the profile model? – King Aug 11 '17 at 14:08
  • thanks. please I got the following error in my browser` Exception Value: global name 'UpdateProfile' is not defined` – King Aug 11 '17 at 14:31
  • 1
    We would need more information to troubleshoot that, but I would hazard to guess that you forgot to import that class? – MrName Aug 11 '17 at 14:33
  • yeah I did that and it fine. then another error saying `global name 'RegistrationForm' ` is not defined then I imported it to my view and I got this error `from .forms import RegistrationForm ImportError: cannot import name RegistrationForm` – King Aug 11 '17 at 14:52
  • Does that class exist in your `forms.py`? – MrName Aug 11 '17 at 14:59
  • no it does not but I am using allauth so I simply assumed it is a predefined class – King Aug 11 '17 at 15:01
1

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

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
  • so there should be no Meta in the model – King Aug 11 '17 at 13:58
  • i got the following error 'TypeError: 'class Meta' got invalid attribute(s): model' – King Aug 11 '17 at 14:00
  • I simply removed the fields. and the `TypeError: 'class Meta' got invalid attribute(s): model` came up – King Aug 11 '17 at 14:01
  • 1
    I edit the answer, in your case need remove class META – Brown Bear Aug 11 '17 at 14:03
  • Ok it works now but if I want to add more fields like Bios and picture upload option . Do I just add the field into the profile model? – King Aug 11 '17 at 14:07
  • Add link to the answer for customize user models – Brown Bear Aug 11 '17 at 14:09
  • please I got the following error in my browser `Exception Value: global name 'UpdateProfile' is not defined` – King Aug 11 '17 at 14:29
  • you need add import the `UpdateProfile` in the place were you catch the error – Brown Bear Aug 11 '17 at 14:37
  • yeah I did that and it fine. then another error saying `global name 'RegistrationForm' is not defined` then I imported it to my view and I got this error `from .forms import RegistrationForm ImportError: cannot import name RegistrationForm` – King Aug 11 '17 at 14:51
1

For your last error need to replace

class UpdateProfile(forms.ModelForm):
    # your code ...
    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        #           ^^^^^^^^^^^^^^^^^

to

class UpdateProfile(forms.ModelForm):
    # your code ...
    def save(self, commit=True):
        user = super(UpdateProfile, self).save(commit=False)
        #           ^^^^^^^^^^^^^^^^^

more details working-python-super-function

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
  • Thanks I cant believe I made such an error which occured when I was trying to fix previous errors – King Aug 11 '17 at 15:04