Saving ListFields and CustomFields to Database - Django

I am very new to Django and I am trying to understand how I can save fields to the db.

I have the following model

class CashFlow(models.Model):
    Id = models.AutoField(primary_key=True)
    CompanyId = models.ForeignKey(Company, on_delete=models.CASCADE)
    PeriodInstant = models.TextField()
    PeriodStartDate = models.TextField()
    PeriodStartDateEndDate = models.TextField()
    Currency = models.TextField()
    Decimals = models.TextField()
    CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents = models.TextField(
        null=True)
    NetIncomeLoss = models.TextField(null=True)

Serializer:

class PeriodSerializer(serializers.Serializer):
    instant = serializers.CharField()
    startDate = serializers.CharField()
    endDate = serializers.CharField()

class CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalentsSerializer(serializers.Serializer):
    decimal = serializers.CharField()
    unit = serializers.CharField()
    period = PeriodSerializer(many=True)
    value = serializers.CharField()


class NetIncomeLossSerializer(serializers.Serializer):
    decimal = serializers.CharField()
    unit = serializers.CharField()
    period = PeriodSerializer(many=True)
    value = serializers.CharField()    
    
class CashFlow(serializers.Serializer):
    CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents = CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalentsSerializer(
        many=True)
    NetIncomeLoss = NetIncomeLossSerializer()

class CashFlowSerializer(serializers.Serializer):
    serializers.DictField(
        child=CashFlow())

I also created a model serializer so that I can map the fields to this model and store them in the database

class CashFlowModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CashFlow
        fields = ('CompanyId',
                  'PeriodInstant',
                  'PeriodStartDate',
                  'PeriodStartDateEndDate',
                  'Currency',
                  'Decimals',
                  'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents',
                  'NetIncomeLoss')

View

if request.method == 'GET':
    jsonData = ''
    data = getData('TestCompany')
    allFilings = itemgetter('filings')(data)
    for item in allFilings:
        linkToFilingDetails = itemgetter('linkToFilingDetails')(item)
        jsonData = get_data(linkToFilingDetails, "StatementsOfCashFlows")
    check = CashFlowSerializer(data=jsonData)
    if (check.is_valid()):
        print("ready to send to db")
    return JsonResponse(jsonData, safe=False)

JSON data = http://jsonblob.com/1063218156159647744

Also, since CompanyId is not a field included in the JSON from API Call, how can I map that and save ?

Thank for you the time

Back to Top