Y
Y
Yakov Feldman2018-06-18 20:40:03
Django
Yakov Feldman, 2018-06-18 20:40:03

Why does a Django form require an object instead of a key?

There is a model and a form

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}
        )

When I try to edit the object, I get the message
Cannot assign "'14'": "Place.location" must be a "Location" instance.Cannot assign "'
and flies away at the moment
if form.is_valid():
I don't understand what's wrong
Thank you

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Bjornie, 2018-06-18
@Bjornie

What are you passing to form.cleaned_data['location']?

T
Tim, 2018-06-18
@darqsat

Because it's a string

Cannot assign "'14'" .

Try converting this to int:
IDs are stored in the database as ints.

K
Konstantin Malyarov, 2018-06-18
@Konstantin18ko

place.location_id = form.cleaned_data['location']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question