Firstly, you've defined your foto
field as foto = ImageField()
it should probably be foto = models.ImageField()
Secondly, when you initiate your form you're not including the instance
argument (have a look at the docs). It should be something like -
form = StepOneForm(request.POST, request.FILES, instance=building)
That's how django knows to add the html form data to an existing instance. If you declare your form like this -
from django.forms import ModelForm
class BuildingForm(ModelForm):
class Meta:
model = Building
Then when you do data = form.save(commit=False)
your data
variable is actually an instance of Building
. And if you've already said that the form relates to a specific instance (eg the building
you declare at the beginning of your view), then data
will be that specific instance of Building
.
This means that you can change your view code to -
def your_view(request):
building = get_object_or_404(Building, pk=b_id)
if request.method == 'POST':
form = BuildingForm(request.POST, request.FILES, instance=building)
if form.is_valid():
building = form.save(commit=False)
building.save()
else: #...