Django - pass a value from form to model - python

i am using Django form module to get value and pass it to user profile.
The only problem is that the home_type is a "forms.ChoiceField" but there is no "clean_data" type for the forms.ChoiceField, so how can i get the radio select value and pass it to the user profile module?
the code of module is like this:
class UserProfile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=30, blank=True)
productNo = models.CharField(max_length=50, blank=True)
location = models.CharField(max_length=50, blank=True)
average_temp = models.CharField(max_length=5, blank=True)
HOME_TYPE = (
('AP', 'Apartment/Condo'),
('SH', 'Single House/Residential'),
('ST', 'Studio'),
('TH', 'Townhouse'),
('MH', 'Moblie Home'),
)
home_type = models.CharField(max_length=1, choices=HOME_TYPE)
and the code of form:
class UserProfileForm(forms.Form):
name = forms.CharField(label="Name", max_length=30, widget=forms.TextInput(attrs={'size': 20,}))
productNo = forms.CharField(label="ProductNo", max_length=50, widget=forms.TextInput(attrs={'size': 20,}))
location = forms.CharField(label="Location", max_length=50, widget=forms.TextInput(attrs={'size': 20,}))
average_temp = forms.CharField(label="Average_Temp", max_length=10, widget=forms.TextInput(attrs={'size': 20,}))
HOME_TYPE = (
('AP', 'Apartment/Condo'),
('SH', 'Single House/Residential'),
('ST', 'Studio'),
('TH', 'Townhouse'),
('MH', 'Moblie Home'),
)
home_type = forms.ChoiceField(label="home_type", widget=forms.RadioSelect(), choices=HOME_TYPE)
In my view.py, the code to deal with the value passing is:
def user_profile_update(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/keenhome/accounts/login/')
UserProfile = request.user.get_profile()
if request.method == 'POST':
form=UserProfileForm(request.POST)
if form.is_valid():
UserProfile.name = form.cleaned_data["name"]
UserProfile.productNo = form.cleaned_data["productNo"]
UserProfile.location = form.cleaned_data["location"]
UserProfile.average_temp = form.cleaned_data["average_temp"]
#problem happens here:
UserProfile.home_type = form.cleaned_data["home_type"]
UserProfile.save()
return HttpResponseRedirect('/keenhome/accounts/user_profile/')
else:
form = UserProfileForm()
return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
else:
form = UserProfileForm()
return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
The only problem is that there is no "clean_data" type for the forms.ChoiceField, so how can i get the radio select value and pass it to the user profile module?

You don't need to write all fields of form again. That's why it's called ModelForm, it should be auto-generated from model:
HOME_TYPE = (
('AP', 'Apartment/Condo'),
('SH', 'Single House/Residential'),
('ST', 'Studio'),
('TH', 'Townhouse'),
('MH', 'Moblie Home'),
)
class UserProfile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=30, blank=True)
productNo = models.CharField(max_length=50, blank=True)
location = models.CharField(max_length=50, blank=True)
average_temp = models.CharField(max_length=5, blank=True)
home_type = models.CharField(max_length=2, choices=HOME_TYPE)
forms.py:
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
exclude = (user,)
views.py:
def user_profile_update(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/keenhome/accounts/login/')
form = UserProfileForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
form.user = request.user
form.save()
return HttpResponseRedirect('/keenhome/accounts/user_profile/')
return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
EDIT:
made max_length = 2, thanks #ndpu, excluded user, added user saving in form, thanks #void

class UserProfile(models.Model):
user = models.OneToOneField(User)
.....
class UserProfileForm(ModelForm):
pass
class Meta:
model = UserProfile
exclude = (user,) # you need to exclude user
Now all of this code...:
def user_profile_update(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/keenhome/accounts/login/')
UserProfile = request.user.get_profile()
if request.method == 'POST':
form=UserProfileForm(request.POST)
if form.is_valid():
UserProfile.name = form.cleaned_data["name"]
UserProfile.productNo = form.cleaned_data["productNo"]
UserProfile.location = form.cleaned_data["location"]
UserProfile.average_temp = form.cleaned_data["average_temp"]
#problem happens here:
UserProfile.home_type = form.cleaned_data["home_type"]
UserProfile.save()
return HttpResponseRedirect('/keenhome/accounts/user_profile/')
else:
form = UserProfileForm()
return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
else:
form = UserProfileForm()
return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))
would have become:
def user_profile_update(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/keenhome/accounts/login/')
form = UserProfileForm(request.POST or None)
if form.is_valid():
form = form.save(commit=False)
form.user = request.user# because user is excluded
form.save()
#code
else:
#code
return render_to_response("polls/user_profile_update.html", {'form':form}, context_instance=RequestContext(request))

I think the problem is here:
class UserProfile(models.Model):
#...
home_type = models.CharField(max_length=1, choices=HOME_TYPE)
should be max_length=2:
home_type = models.CharField(max_length=2, choices=HOME_TYPE)

Related

Django save form with foreign key

I am currently trying to create a form where users get to fill in their details after creating an account. The idea is that every user, after registration, gets redirected to this form page to fill it out. To achieve this, i'm using foreign keys.However it doesn't save to database.
models.py
class User(AbstractUser):
pass
def __str__(self):
return self.username
class Detail(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False, default="")
first_name = models.CharField(max_length=200, default="")
last_name = models.CharField(max_length=255, default="")
class Meta:
verbose_name_plural = "Detail"
def __str__(self):
return self.first_name+ " "+self.last_name
forms.py
class Details(forms.ModelForm):
class Meta:
model = Detail
fields = "__all__"
widgets={
"user": forms.TextInput()
}
views.py
def details(request):
if request.method =="POST":
form = Details(request.POST)
if form.is_valid():
detail = form.save(commit=False)
detail.user = request.user
detail.first_name = detail.first_name.lower()
detail.last_name = detail.last_name.lower()
detail.save()
return redirect("admin:index")
else:
form = Details(initial={"user":request.user.username})
return render(request, "details.html", {"form":form})
You need to exclue user field from ModelForm like this
form.py
class Details(forms.ModelForm):
class Meta:
model = Detail
fields = "__all__"
exclude =["user"]
views.py
def details(request):
if request.method =="POST":
form = Details(request.POST)
if form.is_valid():
detail = form.save(commit=False)
detail.user = request.user
detail.first_name = detail.first_name.lower()
detail.last_name = detail.last_name.lower()
detail.save()
return redirect("admin:index")
else:
form = Details()
return render(request, "details.html", {"form":form})

Django model stoped working when adding more models

I have a view in which the user can edit their profile everything worked fine and everything was being updated (biography, first_name, username, email and profile-picture) but now that I added a new app that contains three views in which the user can upload, delete and like posts, the user update sistem stoped working for some reason just the (update, email, first_name)still worked. The update view calls 2 models, User that lets you edit(name, username and email) and Profile that lets you edit(bio and change the profile pictures) it looks like when I added the upload, delete and like functions mentioned before, the whole Profile model "disapeared" even tho is there. The error I am getting is RelatedObjectDoesNotExist User has no profile.
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_pic = models.ImageField(upload_to='profile_pics', null=True, blank=True, default='default.png')
bio = models.CharField(max_length=400, default=1, null=True)
connection = models.CharField(max_length = 100, blank=True)
follower = models.IntegerField(default=0)
following = models.IntegerField(default=0)
def __str__(self):
return f'{self.user.username} Profile'
class Post(models.Model):
text = models.CharField(max_length=200)
video = models.FileField(upload_to='clips', null=True, blank=True)
user = models.ForeignKey(User, related_name='imageuser', on_delete=models.CASCADE, default='username')
liked = models.ManyToManyField(User, default=None, blank=True, related_name='liked')
updated = models.DateTimeField(auto_now=True)
created =models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.text)
LIKE_CHOICES = (
('Like', 'Like'),
('Unlike', 'Unlike'),
)
class Like(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
value = models.CharField(choices=LIKE_CHOICES, default='Like', max_length=10)
def __str__(self):
return str(self.post)
views.py
def edit_profile(request):
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=request.user)
form1 = UpdateProfileForm(request.POST, request.FILES, instance=request.user.profile)
if form.is_valid and form1.is_valid:
form.save()
form1.save()
return redirect('profile')
else:
form = EditProfileForm(instance=request.user)
form1 = UpdateProfileForm(instance=request.user)
args = {
'form': form,
'form1': form1,
}
return render(request, 'profile-edit.html', args)
urls.py
urlpatterns = [
path('<username>/', views.profile, name='profile'),
path('edit-profile', views.edit_profile, name='edit-profile'),
]
forms.py
class EditProfileForm(UserChangeForm):
class Meta:
model = User
fields = (
'first_name',
'username',
'email',
)
exclude = ('password',)
class UpdateProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = (
'bio',
'profile_pic',
)
If you need to see more code please let me know in the comments.
Add the following in your views before the if statement:
profile, created = Profile.objects.get_or_create(user=request.user)
You need the ", created" since the result of get_or_create will be a tuple not an object - the profile either already exists or must be created.
For your new problem, change the following line:
form1 = UpdateProfileForm(request.POST, request.FILES, instance=request.user.profile)
to:
form1 = UpdateProfileForm(request.POST or None, request.FILES, instance=request.user.profile)

django modelform more then one foreign key using exclude

here user i want user and userprofile. useing exclude. when i add userprofile error 'WSGIRequest' object has no attribute 'userprofile'
modelform
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ('categories', 'title', 'description', 'image', 'price')
exclude = ('user','userprofile')
view
#login_required
def productpost(request):
form = ProductForm()
if request.method == "POST":
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
form = form.save(commit=False)
form.user = request.user
form.userprofile = request.userprofile
form.save()
return success(request)
else:
print("The Form Is Invalid")
return render(request, 'product/postproduct.html', {'form': form})
Model
class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
userprofileinfo = models.ForeignKey(UserProfileInfo, on_delete=models.CASCADE)
categories = models.ForeignKey(Categories, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
description = models.TextField()
image = models.FileField()
price = models.IntegerField()
pub_date = models.DateTimeField('date published', auto_now_add=True)
def __str__(self):
return self.title
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

Django auth.models User One-To-Many (1048, "Column 'user_id' cannot be null")

I'm new to django and my task is to make user able to create a lot profiles after registration when he's logged in.
This is my models.py:
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(blank=False, max_length=100)
lastname = models.CharField( blank=False, max_length=100)
city = models.CharField( blank=True, max_length=100)
country = models.CharField( blank=True, max_length=100)
phonenumber = models.CharField(blank=False, null=True, max_length=20)
email = models.EmailField(blank=False, max_length=100)
date_of_birth = models.DateField( blank=True, null=True)
date_of_addition = models.DateField(auto_now=True, editable=False, null=True)
When I create my user, do login and press create profile button I get :
django.db.utils.IntegrityError: (1048, "Column 'user_id' cannot be null")
This is my views.py:
#login_required
def profile_create(request):
data = dict()
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
form.save()
data['form_is_valid'] = True
profiles = Profile.objects.all()
data['html_profile_list'] = render_to_string('basic_app/users_list_1_10.html', {'profiles': profiles})
else:
data['form_is_valid'] = False
else:
form = ProfileForm()
context = {'form': form}
data['html_form'] = render_to_string('basic_app/partial_profile_create.html', context, request=request)
return JsonResponse(data)
def register(request):
registered = False
if request.method == "POST":
user_form = UserForm(data=request.POST)
if user_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
registered = True
else:
print(user_form.errors)
else:
user_form = UserForm()
return render(request,'basic_app/registration.html',{'user_form':user_form,'registered':registered})
This is my forms.py:
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from .models import Profile
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields = ('name', 'lastname', 'city', 'country', 'phonenumber', 'email', 'date_of_birth')
class UserForm(ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
username = forms.CharField(help_text=False)
class Meta():
model = User
fields = ('username','password')
Thanks
It happens because in your Profile model you have user field which is ForeignKey. In Django ForeignKey is required by default, which means that you can't create new Profile without specifying user field. But you don't provide user in ProfileForm.
You can either provide user in ProfileForm or set user field as not required by adding:
user = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE)
I should request user before saving ProfileForm:
#login_required
def profile_create(request):
data = dict()
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
add = form.save(commit=False)
add.user = request.user
add.save()
data['form_is_valid'] = True
profiles = Profile.objects.all()
data['html_profile_list'] = render_to_string('basic_app/users_list_1_10.html', {'profiles': profiles})
else:
data['form_is_valid'] = False
else:
form = ProfileForm()
context = {'form': form}
data['html_form'] = render_to_string('basic_app/partial_profile_create.html', context, request=request)
return JsonResponse(data)

Django - duplicate key value violates unique constraint

In my Django project i create an app to have additional information about registered users. So my model looks like this:
class UserProfile(models.Model):
class Meta:
verbose_name_plural = u'User Profile'
user = models.OneToOneField(User)
birthday = models.DateField(blank=True, null=True)
avatar = models.ImageField(upload_to='media/profile/avatar', blank=True, null=True)
name = models.CharField(blank=True, null=True, max_length=20)
surname = models.CharField(blank=True, null=True, max_length=50)
phone = models.CharField(blank=True, null=True, max_length=12)
def __unicode__(self):
return '%s' % self.user
Here is my registration form:
class RegistrationForm(ModelForm):
username = forms.CharField(label=(u'Username'))
email = forms.EmailField(label=(u'Email'))
password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
password1 = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
class Meta:
model = UserProfile
exclude = ('user',)
fields = ('username', 'email', 'password', 'password1')
def clean_email(self):
email = self.cleaned_data['email']
try:
User.objects.get(email=email)
except User.DoesNotExist:
return email
raise forms.ValidationError("User with same email already exist, please change your email")
And here is view of my registration form:
def UserProfileRegistration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password=form.cleaned_data['password'])
user.save()
user_profile = UserProfile(user=user)
user_profile.save()
return HttpResponseRedirect('/profile/')
else:
return render(request, 'profiles/registration.html', {'form':form})
else:
form = RegistrationForm()
context = {'form':form}
return render (request, 'profiles/registration.html', context)
In user profile i create modelform where user can fill the fields from UserProfile model:
class ExtraProfileDataForm(ModelForm):
name = forms.CharField(label=(u'Enter your name'))
surname = forms.CharField(label=(u'Enter your surname'))
phone = forms.CharField(label=(u'Enter your phone'))
birthday = forms.DateField(label=(u'Enter birthday'))
avatar = forms.ImageField(label=(u'Enter avatar'))
class Meta:
model = UserProfile
fields = ('name', 'surname', 'phone', 'birthday', 'avatar')
def __init__(self, *args, **kwargs):
super(ExtraProfileDataForm, self).__init__(*args, **kwargs)
for key in self.fields:
self.fields[key].required = False
This is the view of the model form:
#login_required
def UserFullDataForm(request):
if request.method == 'POST':
form = ExtraProfileDataForm(request.POST)
if form.is_valid():
profile_user = request.user
user_profile = UserProfile(user=profile_user)
user_profile.name = form.cleaned_data['name']
user_profile.surname = form.cleaned_data['surname']
user_profile.phone = form.cleaned_data['phone']
user_profile.birthday = form.cleaned_data['birthday']
user_profile.avatar = form.cleaned_data['avatar']
user_profile.save()
return HttpResponseRedirect('/profile/')
else:
return render(request, 'profiles/extra_profile.html', {'form':form})
else:
form = ExtraProfileDataForm()
context = {'form':form}
return render (request, 'profiles/extra_profile.html', context)
But when I fill this form I got the error:
duplicate key value violates unique constraint "profiles_userprofile_user_id_key" DETAIL: Key (user_id)=(23) already exists.
On the traceback i see that error in this line user_profile.save(). As i understand it happens because this script create new item with same id but not update it. What i have to change on my code to update existing item but not to create. Thank you.
You need to use get_or_create, which will return an existing item if it was found; otherwise create a new instance of the model.
In your user profile update view:
user_profile, created = UserProfile.objects.get_or_create(user=profile.user)
Now, instead of creating a new profile each time - you are updating if a profile for that user already exists.

Categories

Resources