Reputation:
For the account type i used AbstractUser to construct my model
class Profile(AbstractUser):
pass
class Second(Profile)
#fields here
class Third(Profile)
#fields here
And the corresponding Forms follows
class UserForm(ModelForm):
class Meta:
model = User
fields = ('username', 'email', 'password')
class SecondForm(ModelForm):
class Meta:
model = Artist
fields = ('artist_name', 'music_genre', 'profile_image')
I don't know this is the right way for define model function
My question now is How to create a User include the second model
or Third model
field.?
I have the HTML
page that user can select which account to create USerType
..
def letsadduser(request):
if request.method == "POST":
form = UserForm(request.POST)
form_second = SecondForm(request.POST, request.FILES)
if form.is_valid() and form_second.is_valid():
User = get_user_model()
new_user = User.objects.create_user(**form.cleaned_data)
Second_user = Second()
Second_user.artist_name = request.POST['artist_name']
Second_user.music_genre = request.POST['music_genre']
Second_user.profile_image = request.FILES['profile_image']
new_user.save()
Second_user.save()
#login(request, new_user)
return render_to_response('welcome.html')
else:
form = UserForm()
form_second = SecondForm()
return render(request, 'registration/test.html', {'form_second': form_second, 'form':form})
The error (1062, "Duplicate entry '' for key 'username'") thrown right now . but Its creating user, i think second form not working.. How to solve this error ?
Upvotes: 1
Views: 294
Reputation: 12333
I saw these lines and immpediately noticed the issue you're having:
class Profile(AbstractUser):
pass
class Second(Profile)
#fields here
class Third(Profile)
#fields here
When you create an user:
User = get_user_model()
new_user = User.objects.create_user(**form.cleaned_data)
You're creating a user.
When you create the second user:
Second_user = Second()
Second_user.artist_name = request.POST['artist_name']
Second_user.music_genre = request.POST['music_genre']
Second_user.profile_image = request.FILES['profile_image']
Second_user.save()
You're not populating the username
field. Perhaps you ran this code at least twice and an empty-user record exists in database.
However the big issue is not what you ask. The big issue is the following:
create
on a User. a User entry is created.save
on a Second instance. Another User entry is created and a Second entry is created.save
on the create
d user. This call has no effect.So you are creating two user entries. You must fix the model if you want to have one User having two profiles. Currently you have:
class Profile(AbstractUser):
pass
class Second(Profile)
#fields here
class Third(Profile)
#fields here
If you don't add fields to AbstractUser: Why don't you just use the default User? (just a convenience; not part of the fix).
User = get_user_model()
You need to make Profiles, and not Subclasses, so the user may be of both profiles. This is a totally distinct OOP concept. So this issue is not about Django but about OOP.
class Second(Model):
user = ForeignKey(User, ...params...)
...fields...
class Third(Model):
user = ForeignKey(User, ...params...)
...fields...
You need to fix your call to instantiate users with Second profile:
User = get_user_model()
new_user = User.objects.create_user(**form.cleaned_data)
Second_user = Second()
Second_user.user = new_user
Second_user.artist_name = request.POST['artist_name']
Second_user.music_genre = request.POST['music_genre']
Second_user.profile_image = request.FILES['profile_image']
Second_user.save()
For god's sake make your view atomic/transactional! You're adding multiple records.
Upvotes: 1