На самом деле я объединил разные таблицы в Django, используя Rest Framework и базу данных MySql.Ответ приходит правильно, но данные из разных таблиц поступают в разные массивы.Но я хочу получить ответ в одном массиве. Я публикую настоящий ответ и требуемый ответ.Пожалуйста, помогите мне с этим.
Ответ будет таким:
{
"count": 282566,
"next": "http://127.0.0.1:8000/inventoryregister/?limit=1000&offset=1000",
"previous": null,
"results": [
{
"inventory_batch": {
"inventory_reg": {
"id": 77,
"company_id": "2",
"inventory_group_id": "39",
"product_id": "834",
"is_owned": "1",
"cost_center": {
"location": "123123213",
"lob_id": 87,
"department_id": 470,
"cs_id": "117",
"nick_name": "123123213",
"lob": {
"lob_name": "ssss",
"lob_nick_name": "ssss",
"cs_id": "0"
},
"department": {
"department_name": "test dept6545",
"department_nick_name": "test dept6",
"cs_id": "44"
}
},
"inventory_date": "2019-04-24",
"inventory_action": "2",
"stock_qty": "100.0",
"price_per_unit": "1000.0",
"total_price": "100000.0",
"status": "1",
"added_by": "3254",
"updated_by": "3254",
"date_added": "2019-04-20 00:26:37",
"date_updated": "2019-04-20 00:27:57",
"product_type2_id": "6",
"asset_type": {
"asset_type": "Fixed Assets"
},
"dimension": null
},
"batch_no": "PRO2175200420192"
},
"barcode_no": "0397622466"
}
Но я хочу получить ответ таким образом:
{
"count": 282566,
"next": "http://127.0.0.1:8000/inventoryregister/?limit=1000&offset=1000",
"previous": null,
"results": [
{
"id": 77,
"company_id": "2",
"inventory_group_id": "39",
"product_id": "834",
"is_owned": "1",
"location": "123123213",
"lob_id": 87,
"department_id": 470,
"cs_id": "117",
"nick_name": "123123213",
"lob_name": "ssss",
"lob_nick_name": "ssss",
"cs_id": "0"
"department_name": "test dept6545",
"department_nick_name": "test dept6",
"cs_id": "44"
"inventory_date": "2019-04-24",
"inventory_action": "2",
"stock_qty": "100.0",
"price_per_unit": "1000.0",
"total_price": "100000.0",
"status": "1",
"added_by": "3254",
"updated_by": "3254",
"date_added": "2019-04-20 00:26:37",
"date_updated": "2019-04-20 00:27:57",
"product_type2_id": "6",
"asset_type": "Fixed Assets"
"dimension": null
"batch_no": "PRO2175200420192"
"barcode_no": "0397622466"
}
}
И этомой код:
view.py
def index(request):
if request.method=='GET':
all_dataobj=inventory_bar_codes.
objects.all().select_related("inventory_batch)
paginator = CustomPagination()
result_page = paginator.paginate_queryset(all_dataobj, request)
pserializer=inventory_bar_codesSerializers(result_page,many=True)
return paginator.get_paginated_response(pserializer.data)
serializer.py
class asset_typeSerializers(serializers.ModelSerializer):
class Meta:
model = asset_type
fields = ['asset_type']
class department_detailsSerializers(serializers.ModelSerializer):
class Meta:
model = department_details
fields = ['department_name','department_nick_name','cs_id']
class lobSerializers(serializers.ModelSerializer):
class Meta:
model = lob
fields = ['lob_name','lob_nick_name','cs_id']
class dimensionsSerializers(serializers.ModelSerializer):
class Meta:
model = dimensions
fields = ['dimension_code','dimension_name']
class locationsSerializers(serializers.ModelSerializer):
lob = lobSerializers(read_only=True)
department = department_detailsSerializers(read_only=True)
class Meta:
model = locations
fields = ['location','lob_id','department_id',
'cs_id','nick_name','lob','department']
class inventoryregisterSerializers(serializers.ModelSerializer):
cost_center = locationsSerializers(read_only=True)
dimension = dimensionsSerializers(read_only=True)
asset_type = asset_typeSerializers(read_only=True)
class Meta:
model = inventoryregister
fields = ['id','company_id','inventory_group_id','product_id',
'is_owned','cost_center','inventory_date','inventory_action',
'stock_qty','price_per_unit','total_price',
'status','added_by','updated_by',
'date_added','date_updated','product_type2_id',
'asset_type','cost_center','dimension','asset_type']
class inventory_batchSerializers(serializers.ModelSerializer):
inventory_reg = inventoryregisterSerializers(read_only=True)
class Meta:
model = inventory_batch
fields = ['inventory_reg','batch_no']
class inventory_bar_codesSerializers(serializers.ModelSerializer):
inventory_batch = inventory_batchSerializers(read_only=True)
class Meta:
model = inventory_bar_codes
fields = ['inventory_batch','barcode_no']
model.py
class asset_type(models.Model):
id = models.IntegerField
company_id = models.CharField(max_length=250)
asset_type = models.CharField(max_length=250)
status = models.CharField(max_length=250)
added_by = models.CharField(max_length=250)
updated_by = models.CharField(max_length=250)
date_added = models.CharField(max_length=250)
date_updated = models.CharField(max_length=250)
class Meta:
db_table = 'asset_type'
class department_details(models.Model):
id = models.IntegerField
department_code = models.CharField(max_length=250)
company_id = models.CharField(max_length=250)
costcenter_id = models.CharField(max_length=250)
department_name = models.CharField(max_length=250)
department_nick_name = models.CharField(max_length=250)
dept_start_date = models.CharField(max_length=250)
added_by = models.CharField(max_length=250)
updated_by = models.CharField(max_length=250)
status = models.CharField(max_length=250)
date_added = models.CharField(max_length=250)
date_updated = models.CharField(max_length=250)
cs_id = models.CharField(max_length=250)
logistic_dept = models.CharField(max_length=250)
class Meta:
db_table = 'department_details'
class lob(models.Model):
id = models.IntegerField
lob_name = models.CharField(max_length=250)
company_id = models.CharField(max_length=250)
client_id = models.CharField(max_length=250)
logo = models.CharField(max_length=250)
lob_brand_name = models.CharField(max_length=250)
lob_start_date = models.CharField(max_length=250)
lob_nick_name = models.CharField(max_length=250)
added_by = models.CharField(max_length=250)
updated_by = models.CharField(max_length=250)
date_added = models.CharField(max_length=250)
date_updated = models.CharField(max_length=250)
status = models.CharField(max_length=250)
lob_status = models.CharField(max_length=250)
cs_id = models.CharField(max_length=250)
class Meta:
db_table = 'lob'
class dimensions(models.Model):
id = models.IntegerField
company_id = models.CharField(max_length=250)
client_id = models.CharField(max_length=250)
dimension_code = models.CharField(max_length=250)
dimension_name = models.CharField(max_length=250)
dimension_type = models.CharField(max_length=250)
description = models.CharField(max_length=250)
country = models.CharField(max_length=250)
state = models.CharField(max_length=250)
city = models.CharField(max_length=250)
location = models.CharField(max_length=250)
address = models.CharField(max_length=250)
pincode = models.CharField(max_length=250)
furnishing_status = models.CharField(max_length=250)
status = models.CharField(max_length=250)
added_by = models.CharField(max_length=250)
updated_by = models.CharField(max_length=250)
date_added = models.CharField(max_length=250)
date_updated = models.CharField(max_length=250)
cs_id = models.CharField(max_length=250)
is_this_warehouse = models.CharField(max_length=250)
representative_name = models.CharField(max_length=250)
representative_mobile = models.CharField(max_length=250)
representative_email = models.CharField(max_length=250)
rack_dimension = models.CharField(max_length=250)
class Meta:
db_table = 'dimensions'
class locations(models.Model):
id = models.IntegerField
cs_id = models.CharField(max_length=250)
company_id = models.CharField(max_length=250)
client_id = models.CharField(max_length=250)
lob = models.ForeignKey(lob,on_delete=models.CASCADE)
location = models.CharField(max_length=250)
nick_name = models.CharField(max_length=250)
gst_no = models.CharField(max_length=250)
address = models.CharField(max_length=250)
state = models.CharField(max_length=250)
city = models.CharField(max_length=250)
pincode = models.CharField(max_length=250)
status = models.CharField(max_length=250)
added_by = models.CharField(max_length=250)
updated_by = models.CharField(max_length=250)
date_added = models.CharField(max_length=250)
date_updated = models.CharField(max_length=250)
start_date = models.CharField(max_length=250)
is_lob_mapped = models.CharField(max_length=250)
is_department_mapped = models.CharField(max_length=250)
department = models.ForeignKey(department_details,on_delete=models.CASCADE)
is_dimension_mapped = models.CharField(max_length=250)
dimension_id = models.CharField(max_length=250)
is_sez = models.CharField(max_length=250)
is_eou = models.CharField(max_length=250)
location_id = models.CharField(max_length=250)
class Meta:
db_table = 'locations'
class inventoryregister(models.Model):
id = models.IntegerField
company_id = models.CharField(max_length=250)
inventory_group_id = models.CharField(max_length=15)
product_id = models.CharField(max_length=250)
is_owned = models.CharField(max_length=250)
asset_type = models.ForeignKey(asset_type,db_column='asset_type',
on_delete=models.CASCADE)
cost_center = models.ForeignKey(locations,on_delete=models.CASCADE)
inventory_date = models.CharField(max_length=250)
inventory_action = models.CharField(max_length=250)
stock_qty = models.CharField(max_length=250)
price_per_unit = models.CharField(max_length=250)
total_price = models.CharField(max_length=250)
status = models.CharField(max_length=250)
added_by = models.CharField(max_length=250)
updated_by = models.CharField(max_length=250)
date_added = models.CharField(max_length=250)
date_updated = models.CharField(max_length=250)
product_type2_id = models.CharField(max_length=250)
dimension = models.ForeignKey(dimensions,on_delete=models.CASCADE)
class Meta:
db_table = 'inventory_register'
class inventory_batch(models.Model):
id = models.IntegerField
company_id = models.CharField(max_length=250)
inventory_reg = models.ForeignKey(inventoryregister,
on_delete=models.CASCADE)
batch_no = models.CharField(max_length=250)
status = models.CharField(max_length=250)
added_by = models.CharField(max_length=250)
updated_by = models.CharField(max_length=250)
date_added = models.CharField(max_length=250)
date_updated = models.CharField(max_length=250)
class Meta:
db_table = 'inventory_batch'
class inventory_bar_codes(models.Model):
id = models.IntegerField
company_id = models.CharField(max_length=250)
inventory_reg_id = models.CharField(max_length=250)
inventory_batch = models.ForeignKey(inventory_batch,
on_delete=models.CASCADE)
barcode_no = models.CharField(max_length=250)
status = models.CharField(max_length=250)
is_available = models.CharField(max_length=250)
qrcode_file = models.CharField(max_length=250)
added_by = models.CharField(max_length=250)
updated_by = models.CharField(max_length=250)
date_added = models.CharField(max_length=250)
date_updated = models.CharField(max_length=250)
class Meta:
db_table = 'inventory_bar_codes'