"'int' object has no attribute 'save'" Update models from views.py - django

I want to update user model in a views.py. Is there a way to do that? What I want to do is minus user point in views.py and update into models without a form. but it gave me " 'int' object has no attribute 'save'"
views.py
def buy_item(request, item_name):
item = ShopItem.objects.get(item_name=item_name)
price = item.item_price
user = request.user
user_point = user.point
if user_point >= price:
user_point = user_point - price
point_left = user_point
point_left.save()
msg = messages.success(request, 'You bought item!')
return redirect('/shop', msg)
else:
msg = messages.success(request, 'You do not have enough point')
context = ({
'point': user_point,
'item': item,
'form': form
})
return render(request, 'item.html', context)
item models.py
class ShopItem(models.Model):
item_name = models.CharField(max_length=16)
item_image = models.ImageField(upload_to='media_items')
item_description = models.CharField(max_length=255)
item_command = models.CharField(max_length=255)
item_price = models.IntegerField(default=0)
def __str__(self):
return self.item_name
def snippet(self):
return self.item_description[:45]
user models.py
class UserProfile(AbstractUser):
username = models.CharField(max_length=16, unique=True)
email = models.EmailField(default='', unique=True)
phone = models.CharField(max_length=10)
point = models.IntegerField(default=0)
password1 = models.CharField(max_length=255)
password2 = models.CharField(max_length=255)
USERNAME_FIELD = 'username'
What I want is to save the point after changed.
What should I do? Please help! I'm really new to this. Sorry for my English.

Sadly, I don't have enough reputation to make a comment, but you need to use save() method in a model, not in an Integer.
So, you should replace
point_left = user_point
point_left.save()
for this
user.point = user_point # Assign the new point to the user
user.save() # Update your user model

Related

model form is not saving in django it is telling user can not be nul

i crated a model name address and connected with user by foreign key so a user can have multiple address but it is not saving i want form to take that user id to save but i don't how to do that
here is my models.py
class Address(models.Model):
phoneNumberRegex = RegexValidator(regex = r"^\d{10,10}$")
pincodeRegex = RegexValidator(regex = r"^\d{6,6}$")
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='address')
reciever_name = models.CharField(max_length=200, blank=False)
phone_no = models.CharField(validators = [phoneNumberRegex], max_length = 10, blank=False)
alt_phone_no = models.CharField(validators = [phoneNumberRegex], max_length = 10, blank=True)
state = models.CharField(max_length=50, choices=state_choice, blank=False)
pincode = models.CharField(validators = [pincodeRegex], max_length = 6, blank=False)
eighteen = models.CharField(blank=False, choices=eighteen_choice, default='Yes', max_length=4 )
city = models.CharField(max_length=100, blank=False)
address = models.CharField(max_length=500, blank=False)
locality = models.CharField(max_length=300, blank=True)
joined_date = models.DateTimeField(default=timezone.now,editable=False)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.user.username
my views.py
#login_required
def add_address(request, username):
if request.method == 'POST':
form = Addressform(request.POST)
if form.is_valid():
form.save()
return redirect('accounts:home')
else:
form = Addressform()
return render(request, 'add_address.html', {'form': form})
my form.py
class Addressform(forms.ModelForm):
class Meta:
model = Address
fields = '__all__'
exclude = ['user', 'joined_date', 'updated_at']
labels = {
'reciever_name':'Reciever Name',
'phone_no':'Phone No',
'alt_phone_no':'Alternate Phone No',
'state':'State/Union Territory',
'pincode':'Area Pincode',
'eighteen':'Is reciever is 18+',
'city':'City',
'address':'Address',
'locality':'Locality',
}
widgets = {
'eighteen': forms.RadioSelect()
}
what i want is in user field it take that user who his login automatically but i don't understand how i can achieve that
since you are excluding the user field from the form, it gets a null value on the post request. With a foreign key you cannot have a null field in the database. You have 2 options:
1) Add the request user
#login_required
def add_address(request, username):
if request.method == 'POST':
form = Addressform(request.POST)
if form.is_valid():
form = form.save(commit=False)
form.user = request.user
form.save(commit=True)
return redirect('accounts:home')
else:
form = Addressform()
return render(request, 'add_address.html', {'form': form})
This will add the primary key of the user who is submitting the form as the foreign key of the address and hence associate the user with the address.
2) Allow user to select the user to associate with the address (not suggested)
The alternative method is to allow the user to select the user you want to associate with the address but it is not suggested as any user can associate any user with a particular address.
class Addressform(forms.ModelForm):
class Meta:
model = Address
fields = '__all__'
exclude = ['joined_date', 'updated_at']
labels = {
'reciever_name':'Reciever Name',
'phone_no':'Phone No',
'alt_phone_no':'Alternate Phone No',
'state':'State/Union Territory',
'pincode':'Area Pincode',
'eighteen':'Is reciever is 18+',
'city':'City',
'address':'Address',
'locality':'Locality',
}
widgets = {
'eighteen': forms.RadioSelect()
}

Getting an Attribute error while trying to alter a related model on a django detailView

I have a CarRent model that is a ForeignKey to the Car model, on a CarRent detailView after payment i want to alter the CarRent model as well as the Car model, the code works well for the CarRent model, but altering the Car model results in “AttributeError at /car/requests/4/
type object 'Car' has no attribute 'object'” error
class Car(models.Model):
car_owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='car_owner', on_delete=models.CASCADE)
car_model = models.CharField(max_length=20, blank=True, null=True)
rented = models.BooleanField(default=False)
class CarRent(models.Model):
car = models.ForeignKey(Car, related_name='rented_car', on_delete=models.CASCADE)
driver = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='driver_renting', on_delete=models.CASCADE)
active_rent = models.BooleanField(default=False, null=True)
rent_paid = models.BooleanField(default=False)
#login_required
#driver_required(redirect_field_name='app:home')
#csrf_protect
def car_rent_detail_view(request, pk):
object = get_object_or_404(CarRent, id=pk)
# queryset = Car.objects.get(id=pk)
car_object = get_object_or_404(queryset, id=pk)
paystack = PaystackAccount(
settings.PAYSTACK_EMAIL,
settings.PAYSTACK_PUBLIC_KEY,
object.total_cost
)
context = {'object': object, 'pk_public': settings.PAYSTACK_PUBLIC_KEY, 'currency': 'NGN', 'paystack': paystack,
}
if request.method == 'POST':
if paystack.verify_transaction(request.POST['reference']):
messages.success(request, "paystack payment successfull")
car_rented = Car.object.get(pk=pk)
car_rented.rented = True
rent_activation = CarRent.objects.get(pk=pk)
rent_activation.active_rent = True
rent_activation.rent_paid = True
rent_activation.save()
messages.success(request, "Your Rent has successfully being updated")
return render(request, 'app/CarRent_detail.html', context=context)
Any help will be appreciated greatly.
Its a typo, you need to use objects to access manager method, not object. So replace:
car_rented = Car.object.get(pk=pk)
^^^^^^
With:
car_rented = Car.objects.get(rented_car__pk=pk)
You can reverse query from Car model to CarRent via related_name(which is 'rented_car'). More information can be found in documentation.

Updating django form without prompting the user to enter their ID

So i'm working on job application portal.
the logic is as follows :
Applicant ---> Applies for ---> Job
Models are (Job, User, Application)
I used the User model from django and i extend it.
Now the dilemma is when i render the ApplicationForm, because i have to update the foreign key and i want it to be updated automatically.
Here is my code :
Models.py
class Job(models.Model):
owner = models.ForeignKey(User,related_name='job_owner',on_delete=models.CASCADE)
title = models.CharField(max_length=100)
#location
job_type = models.CharField(max_length=15,choices=JOB_TYPE)
description= models.TextField(max_length=1000)
published_at = models.DateTimeField(auto_now=True)
vacancy = models.IntegerField(default=1)
salary = models.IntegerField(default=0)
experience = models.IntegerField(default=1)
category = models.ForeignKey('Category',on_delete=models.CASCADE)
icon = models.ImageField(upload_to ='job_icons/',default='job_icons/job.png')
slug = models.SlugField(blank = True,null=True)
class Application(models.Model):
job = models.ForeignKey(Job, related_name="job_applied",on_delete=models.CASCADE)
applicant = models.ForeignKey(User,related_name='job_applicant',on_delete=models.CASCADE)
first_name= models.CharField(max_length=40)
last_name= models.CharField(max_length=40)
email = models.EmailField(max_length=60)
website = models.URLField()
cv = models.FileField(upload_to='application/')
coverletter = models.TextField(max_length=550)
application_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.last_name+"\t"+self.first_name
Forms.py
class JobApplication(ModelForm):
class Meta:
model = Application
fields = ['first_name', 'last_name','email', 'website','cv','coverletter']
vews.py
def job_detail(request,slug):
job_specific = Job.objects.get(slug=slug)
form = JobApplication(instance=request.user)
if request.method == 'POST':
form = JobApplication(request.POST,request.FILES)
if form.is_valid():
my_form = form.save(commit=False)
my_form.job = job_specific
Application.applicant.user = request.user
Application.job = job_specific
my_form.save()
context ={'job_specific':job_specific, 'form':form,}
return render(request,"job/job_details.html",context)
So once the user submit their application, i wanted to updated the fields that are "foreign key" without prompting the user.
I do not know how to arrange this in the views.py or if it's even possible this way?
thanks to everyone in advance
So i solved the problem, it was really simple solution:
my_form = form.save(commit=False)
my_form.job = job_specific
my_form.applicant = request.user

Update Django Model

How to i update a certain value in the database?
class Userdata(models.Model):
user = models.OneToOneField(User, on_delete= models.CASCADE)
faculty = models.ForeignKey(Fakultas,on_delete=models.CASCADE,default= 1)
is_voted = models.BooleanField(default=False)
def __str__(self):return self.user.username
class Voting(models.Model):
name = models.CharField(max_length=50)
faculty = models.ForeignKey(Fakultas, on_delete=models.CASCADE, default=1)
pic = models.CharField(max_length=50)
text = models.TextField()
voters = models.IntegerField()
def __str__(self): return self.name
My Views :
def voted(response):
if response.method == 'POST':
id = response.POST.get['idcalon']
user = Userdata.objects.get() #get the username
calon2 = Voting.objects.get() #get user selection in html
user.is_voted = True
calon2.voters +=1
user.save(['is_voted'])
calon2.save(['voters'])
I'm trying to grab the user's name and then when update the user's is_voted value to True when triggered.
Then, I wanted to grab my Voting model by id, for example, I wanted to edit id = 1
So how do I do it? I've been trying to understand the documentation, but still have 0 idea how to do it.
Thank you
def voted(request):
if request.method == 'POST':
id = request.POST.get['idcalon']
user = Userdata.objects.get(id=request.user.id) #get the username
calon2 = Voting.objects.get(id=id) #get user selection in html
user.is_voted = True
calon2.voters +=1
user.save()
calon2.save()
this will work if user is authenticated

How to intercept and control saving a Django POST form?

When the user is required to fill his profile, he picks a city from the Google Places Autocomplete and posts the form, in the view I extract the city Id from the Google API based on the posted text (I use the same id as pk in my db) and try to extract a city from my db.
These are the models:
class City(models.Model):
#extracted from the Google API
city_id = models.CharField(primary_key=True, max_length=150)
name = models.CharField(max_length=128, blank=True)
country = models.CharField(max_length=128, blank=True)
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile', primary_key=True)
city = models.ForeignKey(City, blank=True, null=True)
prof_pic = models.ImageField(blank=True, upload_to='profile_pictures')
This is the view:
def createprofile(request):
if request.method =='POST':
user = User.objects.get(username=request.user.username)
user_form = UserForm(data=request.POST, instance=user)
profile_form = UserProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.save()
profile = profile_form.save(commit=False)
profile.user = user
#brings back the city search result as text
searched_city = request.POST['city']
#brings back city ID from the Google API
searched_city_id = population_script.get_city_json(searched_city.replace(" ", ""))['results'][0]['id']
#If it's a valid city
if searched_city_id != -1:
city = City.objects.get(city_id = searched_city_id)
profile.city = city#this is what I want to happen!
else:
return HttpResponse("There's no such city, please try a different query.")
if 'prof_pic' in request.FILES:#now save the profile pic
profile.prof_pic = request.FILES['prof_pic']
print("PROF PIC IS: " + profile.prof_pic.url)
else:
profile.prof_pic = 'images/anon.png'
profile.save()
if 'next' in request.GET:
return redirect(request.GET['next'])
else:
print (user_form.errors, profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render(request,
'excurj/createprofile.html', {'user_form':user_form, 'profile_form':profile_form})
However, I keep receiving an error that what's been posted is just text while the city needs to be a City object. I can save the profile pic ok though.
Cannot assign "'Dubai - United Arab Emirates'": "UserProfile.city"
must be a "City" instance.
edit: these are the forms:
class UserForm(forms.ModelForm):
first_name = forms.CharField(
label = "First Name:",
max_length = 80,
required = True
)
last_name = forms.CharField(
label = "Last Name:",
max_length = 80,
required = True,
)
class Meta:
model = User
fields = ('first_name', 'last_name')
class UserProfileForm(forms.ModelForm):
city = forms.CharField(
label = "Your Current City:",
max_length = 200,
required = True,
)
class Meta:
model = UserProfile
fields = ('city','prof_pic', 'dob', 'sex', 'education', 'career', 'about_you',
'music_movies_books', )
Please provide a related_name to the city field in the UserProfile.
I worked around this by creating a new UserProfile field called city_search_text which saves the searched text thus it of course does not return any error. I then receive it in the POST request and comfortable pull the proper city in the view.
I handled a similar issue by overriding my forms' clean method. Something like the following will work:
def clean(self):
# fix city problem
if self.cleaned_data.get("city") is not None:
self.cleaned_data['city'] = City.objects.get(id=self.cleaned_data.get("city"))
return self.cleaned_data