Can I get third party API fields as Foreign key to my Django Model?
NickName:Teshie Ethiopia Ask DateTime:2022-03-02T17:08:46

Can I get third party API fields as Foreign key to my Django Model?

That's true we can make foreign key relationships between models in Django. How about grabbing some fields from third party API fields as Foreign key for a specific Django model?

Here is my Django model budiness_process.py

class BusinessImpact(models.Model, ModelWithCreateFromDict):

client = models.ForeignKey(
    accounts_models.Client, on_delete=models.CASCADE)
business_process = models.ForeignKey(
    BusinessProcess, on_delete=models.CASCADE)
hierarchy = models.CharField(max_length=255)
business_assets = models.CharField(max_length=255)
asset_name = models.CharField(max_length=255)
vendors = models.CharField(max_length=255)
product = models.CharField(max_length=255)
version = models.CharField(max_length=10)
cpe = models.CharField(max_length=255)
asset_type = models.CharField(max_length=10)
asset_categorization = models.CharField(max_length=255)
asset_risk = models.CharField(max_length=50)
_regulations = models.TextField(blank=True)
_geolocation = models.TextField(blank=True)

def __str__(self) -> str:
    return self.hierarchy + " - " + self.business_assets

Here is my serializer.py

class BusinessImpactSerializer(serializers.ModelSerializer):
business_process = BusinessProcessListSerializer()
class Meta:
    model = models.BusinessImpact
    fields = "__all__"

Here is third API implementation for retrieving some of it's fields.

 @api_view(
    ["GET"],
)
def cve_summery(request, key):
    r = requests.get(
        "https://services.nvd.nist.gov/rest/json/cves/1.0?cpeMatchString={}".format(
            key)
    )
    if r.status_code == 200:
        result = []
        res = r.json().get("result").get("CVE_Items")
        for rs in res:
            data = {
                "VulnID": rs.get("cve").get("CVE_data_meta").get("ID"),
                "Summery": rs.get("cve").get("description").get("description_data"),
                "exploitabilityScore": rs.get("impact")
                .get("baseMetricV2")
                .get("exploitabilityScore"),
                "severity": rs.get("impact").get("baseMetricV2").get("severity"),
                "impactScore": rs.get("impact").get("baseMetricV2").get("impactScore"),
            }
            result.append(data)
        return Response(result)
    return Response("error happend", r.status_code)

So my intention here is, can I get "severity": rs.get("impact").get("baseMetricV2").get("severity") as Foreign key in my BusinessImpact model?

Thanks!

Copyright Notice:Content Author:「Teshie Ethiopia」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71320039/can-i-get-third-party-api-fields-as-foreign-key-to-my-django-model

More about “Can I get third party API fields as Foreign key to my Django Model?” related questions

Can I get third party API fields as Foreign key to my Django Model?

That's true we can make foreign key relationships between models in Django. How about grabbing some fields from third party API fields as Foreign key for a specific Django model? Here is my Django ...

Show Detail

How do i implement both built-in django auth and third party social-auth in my application?

I using django from last 4-5 months and recently started learning django-rest-framework and I'm confused about proper authentication system, Actually I am trying to build an application mostly usin...

Show Detail

Django model with dynamic fields based on foreign key

I am trying to implement a model where only a subset of the fields should be presented in forms, based on a foreign key in the model. The total number of fields is relatively low (~20), but may cha...

Show Detail

Django insert data to model with foreign key

I am using django models and am running into issues. I have a couple of models that I am trying to load data to, one with a foreign key to another. I will use two models as an example, but the hope...

Show Detail

Django subclass a third party app for User model

Say I have some third party application that I want to subclass, lets call it ThirdPartyClass. I want to subclass this to create a genereal user, and then have the flexibility to subclass the gene...

Show Detail

Django: Creating a model for a third party class

What is a good way for adding a Django model for an existing third party (non-Django) Python class? I tried multiple inheritance like so: class ThirdPartyClass(object): pass class

Show Detail

Get fields of foreign key in serializer django

I'm 2 weeks into django so this might be dumb! In my django project, I have 3 models, Teacher, Student and a CustomUser. CustomUser has 2 fields first_name and last_name. Student has 3 fields grade,

Show Detail

Django using id to write foreign key fields

I need to have a CRUD interface for interconnected django models. Can i use _id fields to fill in ForeignKey fields? i.e. will this work: class MyModel(models.Model): foreign_key = ForeignKey('

Show Detail

Django: How to join columns without foreign key with ORM

I´m new to Django and Python and I can´t get a solution to this problem, so any help is appreciated! I´m trying to join Database columns in Django, that do have a corresponding value but that valu...

Show Detail

django value of foreign key

Looping over a list of records, Django displays the literal value of each record. But, what if the field is a foreign key? So, here is an example {% for record in records %} {{ record.get_fi...

Show Detail