Object has no attribute 'title'. Updating Many to many field
am trying to create a form where by a user can upload image. The image and the Item have different form i join them together. The challenge is that Image is related to Item with manaytomyfield. i want after a user saved this form, the items field in images table to be updated to the title field in Items table i try to get this from django document but i realize that the document explaination is static but in my case i want it to be dynamic. this is my view
def itemupload(request):
message = 'Invalid Form'
# success = 'Success'
user= request.user
items = Items.objects.all()
if request.method =='POST':
imageform = ImageForm(request.POST, request.FILES)
form =ItemsForm(request.POST, request.FILES)
if form.is_valid() and imageform.is_valid:
item =form.save(commit=False)
item.user =request.user
item.save()
images=imageform.save(commit=False)
**images.save()
images.items.title.add(item)**
return redirect('index')
return message
imageforms =ImageForm()
itemsform = ItemsForm
context = {
'imageforms': imageforms,
'itemsform':itemsform,
}
return render(request, 'jijiapp/itemform.html', context)
this is the model
class Items(models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE, related_name='useritem')
title = models.CharField(max_length=10, null=True, blank =True)
price = models.CharField(max_length=20, null=True, blank=True)
phone = models.CharField(max_length=20, null=True, blank=True)
location =models.CharField(max_length=20, null=True, blank=True)
post_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
def __str__(self):
return self.title
class Meta:
ordering =['title',]
# verbos_name ='Items'
class Images(models.Model):
image =models.ImageField(upload_to='media/', null=True, blank=True)
items = models.ManyToManyField(Items)
def __str__(self):
return str(self.image)
the Traceback
Traceback (most recent call last):
File "C:\Users\Christopher\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Christopher\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Christopher\jijiclone\jijiapp\views.py", line 43, in itemupload
images.items.title.add(item)
AttributeError: 'ManyRelatedManager' object has no attribute 'title'
[22/Jan/2023 01:06:54] "POST /home/itemupload/ HTTP/1.1" 500 66208
enter code here