Django and Angular. I have models Recipe
and Instructions
. I have a dynamic table in Angular that I am using to build out the instructions. I want to be able to add a photo to each instruction. I am not sure how to build this array correctly...
I am using FormData()
because thats the only way I have been able to upload images before. I also tried using an array of Files
models.py
class Recipe(models.Model):
name = models.CharField(max_length=30)
class Instructions(models.Model):
number = models.IntegerField()
explanation = models.TextField(max_length=1000)
recipe = models.ForeignKey(Recipe, related_name='instructions', on_delete=models.CASCADE)
photo = models.ImageField(upload_to='test', null=True, blank=True)
component.ts
newrecipe: RecipeFull = new RecipeFull
instructionArray: Array<Instruction> = [];
newinstruction: any = {}
step:number
fileData: File[]= [];
imageData = new FormData()
instructionArrayPhoto: Array<Instruction>= []
ngOnInit() {
this.step = 1
this.newinstruction = {number: this.step, explanation:"", photo: File };
this.instructionArray.push(this.newinstruction);
}
fileProgress(fileInput: any, row) {
this.fileData[row] = <File>fileInput.target.files[0];
this.imageData.append(row, this.fileData[row])
}
submitrecipe(){
this.newrecipe.instructions = this.instructionArray //this should be the image array
//NOT SURE WHAT TO DO HERE OR HOW TO CREATE THE ARRAY WITH IMAGES...
this.newrecipe.instructions[0].photo = this.imageData[0]
this.recipeservice.addrecipe(this.newrecipe).subscribe()
}
html
<td>
<h3>{{instructionArray[i].number}}</h3>
</td>
<td>
<input [(ngModel)]="instructionArray[i].explanation" class="form-control" type="text" />{{i}}
</td>
<td>
<div>
<div class="form-group">
<input type="file" name="image" (change)="fileProgress($event, i)" />
</div>
</div>
</td>