我的问题是,我有两个模型是产品和税收产品。在这里,我提到了一对多关系,我将产品ID映射到taxproduct。在这个组件中.ts Save_user是一个产品服务。而Save_tax是一种税收产品。首先,我想插入产品,我想将该ID与TAxproduct一起映射。
component.ts
createNewProduct(productForm: NgForm) {
this.productService.save_user(productForm.value)
.subscribe(response => {
const toast_parameter = this.notificationService.getToast('success', 'New Product', 'Inserted Successfully');
this.toastConfig = toast_parameter.config;
this.toasterService.popAsync(toast_parameter.toast);
},
error => {
const toast_parameter = this.notificationService.getToast('error', 'New Product', 'Error Occurred!!');
this.toastConfig = toast_parameter.config;
this.toasterService.popAsync(toast_parameter.toast);
});
for (let i = 0; i < this.contacts.length; i++) {
console.log(this.contacts[i])
this.productService.save_tax(JSON.stringify(this.contacts[i] )).subscribe(response => {
const toast_parameter = this.notificationService.getToast('success', 'New Product', 'Inserted Successfully');
this.toastConfig = toast_parameter.config;
this.toasterService.popAsync(toast_parameter.toast);
},
error => {
const toast_parameter = this.notificationService.getToast('error', 'New Product', 'Error Occurred!!');
this.toastConfig = toast_parameter.config;
this.toasterService.popAsync(toast_parameter.toast);
});
}
}
Model.py
class Product(models.Model):
image = models.ImageField(upload_to='myphoto/%Y/%m/%d/', null=True, max_length=255)
pro_name = models.CharField(max_length=25)
description = models.CharField(max_length=150)
category = models.ForeignKey(Category,on_delete=models.CASCADE)
sales = models.CharField(max_length=25)
cost = models.CharField(max_length=25)
taxable = models.BooleanField(default=False, blank=True)
tax_details= models.CharField(max_length=250)
type_units = models.ForeignKey(Units, on_delete=models.CASCADE)
hsn = models.CharField(max_length=10)
class Taxproduct(models.Model):
tax_name = models.CharField(max_length=50)
tax_percentage = models.CharField(max_length=3)
product_id = models.ForeignKey(Product, on_delete=models.CASCADE)
答案 0 :(得分:0)
如果我了解您问题的某些部分,您可以在第一个服务的订阅方法中调用第二个服务。如果您希望从http响应返回的内容创建组件,您将等待创建组件以使http响应返回。