Есть модель и форма
class Place(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
code = models.CharField(max_length=15, default='1')
name = models.CharField(max_length=50,blank=False, null=False)
face1 = models.ImageField(upload_to='uploads/%Y/%m/%d',
blank=True,
null=True,
)
face2 = models.ImageField(upload_to='uploads/%Y/%m/%d',
blank=True,
null=True,
)
face3 = models.ImageField(upload_to='uploads/%Y/%m/%d',
blank=True,
null=True,
)
location = models.ForeignKey(Location,on_delete=models.CASCADE,null=True)
letter = models.TextField(max_length=250,blank=True,null=True)
web = models.URLField(default="", blank=True, null=True)
def __str__(self):
return self.code + ' by ' + self.user.get_username()
class PlaceForm(forms.ModelForm):
class Meta:
model = Place
fields = ['code','name','location','letter','web']
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super(PlaceForm, self).__init__(*args, **kwargs)
self.fields["location"] = forms.ChoiceField(
choices=[(choice.pk, choice) for choice
in Location.objects.filter(user=user)]
,label='адрес')
def place_ed(request,place_id):
place = Place.objects.get(id=place_id)
if request.method == "POST":
form = PlaceForm(request.POST,user=request.user)
if form.is_valid():
place.code = form.cleaned_data['code']
place.name = form.cleaned_data['name']
place.location = form.cleaned_data['location']
place.letter = form.cleaned_data['letter']
place.web = form.cleaned_data['web']
place.save()
return obj(request)
else:
print(form.errors.as_data())
return msg(request,'bad form')
else:
form = PlaceForm(
initial={
'code':place.code,
'name':place.name,
'location':place.location,
'letter':place.letter,
'web':place.web
},user=request.user
)
return render(request,'place2.html',
{'form':form}
)
При попытке отредактировать объект выдает сообщение
Cannot assign "'14'": "Place.location" must be a "Location" instance.Cannot assign "'
причем улетает в момент
if form.is_valid():
Не понимаю в чем тут дело
Спасибо