Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- models.py
- from django.db import models
- from django.contrib.auth.models import User,AbstractUser
- from django.conf import settings
- from django.dispatch import receiver
- from django.db.models.signals import post_save
- class User(AbstractUser):
- USER_TYPE_CHOICES = (
- (1,'Student'),
- (2,'Teacher'),
- (3,'Stuff')
- )
- profile = models.OneToOneField(on_delete=models.CASCADE, related_name='user')
- user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)
- class Profile(models.Model):
- user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, related_name='profile')
- photo = models.ImageField(upload_to='users/%Y/%m/%d/')
- def __str__(self):
- return 'Profile {}'.format(self.user.username)
- from django import forms
- from django.contrib.auth.models import User
- from .models import Profile
- class UserCreationForm(forms.ModelForm):
- password = forms.CharField(widget=forms.PasswordInput, label='Password')
- password2 = forms.CharField(widget=forms.PasswordInput, label='Repeat Password')
- class Meta:
- model = User
- fields = ('username', 'first_name', 'last_name')
- def clean_password(self):
- cd = self.cleaned_data
- if cd['password']!=cd['password2']:
- raise forms.ValidationError('password no match')
- return cd['password2']
- class ProfileCreationForm(forms.ModelForm):
- class Meta:
- model = Profile
- fields = ('photo',)
- from django.shortcuts import render
- from .forms import ProfileCreationForm, UserCreationForm
- from .models import Profile
- from django.http import HttpResponse
- def sign_up(request):
- if request.method == 'POST':
- user_form = UserCreationForm(instance=request.user,data=request.POST)
- profile_form = ProfileCreationForm(instance=request.user.profile,data=request.POST,
- files=request.FILES)
- if user_form.is_valid() and profile_form.is_valid():
- new_user = user_form.save(commit=False)
- new_user.set_password(user_form.cleaned_data['password'])
- new_user.save()
- Profile.objects.create(user=new_user)
- return HttpResponse('user created')
- else:
- user_form = UserCreationForm()
- profile_form = ProfileCreationForm()
- return render(request, '',{'user_form':user_form, 'profile_form':profile_form})
- Traceback (most recent call last):
- File "manage.py", line 15, in <module>
- execute_from_command_line(sys.argv)
- File "C:UsersMahdiAppDataLocalProgramsPythonPython37libsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
- utility.execute()
- File "C:UsersMahdiAppDataLocalProgramsPythonPython37libsite-packagesdjangocoremanagement__init__.py", line 357, in execute
- django.setup()
- File "C:UsersMahdiAppDataLocalProgramsPythonPython37libsite-packagesdjango__init__.py", line 24, in setup
- apps.populate(settings.INSTALLED_APPS)
- File "C:UsersMahdiAppDataLocalProgramsPythonPython37libsite-packagesdjangoappsregistry.py", line 112, in populate
- app_config.import_models()
- File "C:UsersMahdiAppDataLocalProgramsPythonPython37libsite-packagesdjangoappsconfig.py", line 198, in import_models
- self.models_module = import_module(models_module_name)
- File "C:UsersMahdiAppDataLocalProgramsPythonPython37libimportlib__init__.py", line 127, in import_module
- return _bootstrap._gcd_import(name[level:], package, level)
- File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
- File "<frozen importlib._bootstrap>", line 983, in _find_and_load
- File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
- File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
- File "<frozen importlib._bootstrap_external>", line 728, in exec_module
- File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
- File "C:UsersMahdiPycharmProjectsMyProjectaccountsmodels.py", line 7, in <module>
- class User(AbstractUser):
- File "C:UsersMahdiPycharmProjectsMyProjectaccountsmodels.py", line 13, in User
- profile = models.OneToOneField(on_delete=models.CASCADE, related_name='user')
- TypeError: __init__() missing 1 required positional argument: 'to'
Add Comment
Please, Sign In to add comment